-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewController.swift
More file actions
82 lines (65 loc) · 2.57 KB
/
ViewController.swift
File metadata and controls
82 lines (65 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
//
// AppDelegate.swift
// Sumit's Notes
//
// Created by Sumit Agrawal on 8/22/18.
// Copyright © 2018 Sumit Agrawal. All rights reserved.
//
import UIKit
class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate {
@IBOutlet weak var table: UITableView!
var data:[String] = []
override func viewDidLoad() {
super.viewDidLoad()
table.dataSource = self
table.delegate = self
self.title = "Sumit's Notes"
let addButton = UIBarButtonItem(barButtonSystemItem: .add, target: self, action: #selector(addNote))
self.navigationItem.rightBarButtonItem = addButton
self.navigationItem.leftBarButtonItem = editButtonItem
load()
}
@objc func addNote() {
if table.isEditing {
return
}
let name:String = "Sumit's Item \(data.count + 1)"
data.insert(name, at: 0)
let indexPath:IndexPath = IndexPath(row: 0, section: 0)
table.insertRows(at: [indexPath], with: .automatic)
self.performSegue(withIdentifier: "detail", sender: nil)
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return data.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:UITableViewCell = tableView.dequeueReusableCell(withIdentifier: "cell")!
cell.textLabel?.text = data[indexPath.row]
return cell
}
override func setEditing(_ editing: Bool, animated: Bool) {
super.setEditing(editing, animated: animated)
table.setEditing(editing, animated: animated)
}
func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
data.remove(at: indexPath.row)
table.deleteRows(at: [indexPath], with: .fade)
save()
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "detail", sender: nil)
}
func save() {
UserDefaults.standard.set(data, forKey: "notes")
}
func load() {
if let loadedData:[String] = UserDefaults.standard.value(forKey: "notes") as? [String] {
data = loadedData
table.reloadData()
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}