|
8 | 8 |
|
9 | 9 | import UIKit |
10 | 10 |
|
11 | | -class ViewController: UIViewController { |
| 11 | +class ViewController: UIViewController, UITableViewDataSource { |
| 12 | + |
| 13 | + // MARK: - Variables |
| 14 | + |
| 15 | + /// CoreData connection variable |
| 16 | + let coreDataDB = (UIApplication.sharedApplication().delegate as! AppDelegate).managedObjectContext! |
| 17 | + |
| 18 | + /// TableView array data |
| 19 | + var items = [Item]() |
| 20 | + |
| 21 | + // MARK: - Outlets |
| 22 | + |
| 23 | + // TableView Outlet |
| 24 | + @IBOutlet weak var tableView: UITableView! |
12 | 25 |
|
| 26 | + // MARK: - View Lifecycle |
| 27 | + |
13 | 28 | override func viewDidLoad() { |
14 | 29 | super.viewDidLoad() |
15 | | - // Do any additional setup after loading the view, typically from a nib. |
| 30 | + |
| 31 | + // NavigationController title |
| 32 | + title = "My item list" |
| 33 | + tableView.registerClass(UITableViewCell.self, forCellReuseIdentifier: "Cell") |
| 34 | + |
| 35 | + // Load CoreData data |
| 36 | + items = Item.fetchAll(coreDataDB) |
16 | 37 | } |
| 38 | + |
| 39 | + // MARK: - Actions |
| 40 | + |
| 41 | + /// TableView add button |
| 42 | + @IBAction func addButton(sender: UIBarButtonItem) { |
| 43 | + var addFormAlert = UIAlertController(title: "New item", message: "Enter a name for the item", preferredStyle: .Alert) |
| 44 | + let saveButton = UIAlertAction (title: "Save", style: .Default) { (action: UIAlertAction!) -> Void in |
| 45 | + let nameTextField = (addFormAlert.textFields![0] as! UITextField).text |
| 46 | + |
| 47 | + // Check if name is empty |
| 48 | + if (nameTextField.isEmpty) { |
| 49 | + self.alertError("Unable to save", msg: "Item name can't be blank.") |
| 50 | + } |
| 51 | + // Check duplicate item name |
| 52 | + else if (Item.checkDuplicate(nameTextField, inManagedObjectContext: self.coreDataDB)) { |
| 53 | + self.alertError("Unable to save", msg: "There is already an item with name: \(nameTextField).") |
| 54 | + } |
| 55 | + // Save data |
| 56 | + else { |
| 57 | + // Use class "Item" to create a new CoreData object |
| 58 | + var newItem = Item(name: nameTextField, inManagedObjectContext: self.coreDataDB) |
| 59 | + |
| 60 | + // Add item to array |
| 61 | + self.items.append(newItem) |
| 62 | + |
| 63 | + // CoreData save |
| 64 | + newItem.save(self.coreDataDB) |
| 65 | + |
| 66 | + // Reload Coredata data |
| 67 | + self.items = Item.fetchAll(self.coreDataDB) |
| 68 | + |
| 69 | + // Reload TableView |
| 70 | + self.tableView.reloadData() |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + // No action, no need handler |
| 75 | + let cancelButton = UIAlertAction(title: "Cancel", style: .Destructive, handler: nil) |
17 | 76 |
|
18 | | - override func didReceiveMemoryWarning() { |
19 | | - super.didReceiveMemoryWarning() |
20 | | - // Dispose of any resources that can be recreated. |
| 77 | + // Show textField in alert |
| 78 | + addFormAlert.addTextFieldWithConfigurationHandler { textField in |
| 79 | + textField.placeholder = "Item name" |
| 80 | + } |
| 81 | + |
| 82 | + addFormAlert.addAction(saveButton) |
| 83 | + addFormAlert.addAction(cancelButton) |
| 84 | + |
| 85 | + presentViewController(addFormAlert, animated: true, completion: nil) |
21 | 86 | } |
| 87 | + |
| 88 | + // MARK: - UITableViewDataSource |
| 89 | + |
| 90 | + // TableView rows |
| 91 | + func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { |
| 92 | + return items.count |
| 93 | + } |
| 94 | + |
| 95 | + // Show data in TableView row |
| 96 | + func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { |
| 97 | + let cell = tableView.dequeueReusableCellWithIdentifier("Cell", forIndexPath: indexPath) as! UITableViewCell |
| 98 | + |
| 99 | + cell.textLabel?.text = self.items[indexPath.row].name |
| 100 | + return cell |
| 101 | + } |
| 102 | + |
| 103 | + // Allow edit cell in TableView |
| 104 | + func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool { |
| 105 | + return true |
| 106 | + } |
| 107 | + |
| 108 | + // Allow actions on cell swipe in TableView |
| 109 | + func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) { |
| 110 | + } |
| 111 | + |
| 112 | + // Actions on cell swipe in TableView |
| 113 | + func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? { |
| 114 | + var editSwipeButton = UITableViewRowAction(style: .Default, title: "Edit") { (action, indexPath) in |
| 115 | + let currentName = self.items[indexPath.row].name |
| 116 | + |
| 117 | + var alertEdit = UIAlertController(title: "Editing item", message: "Change item name", preferredStyle: .Alert) |
| 118 | + let saveButton = UIAlertAction(title: "Save", style: .Default) { (action) in |
| 119 | + let newItemName = (alertEdit.textFields![0] as! UITextField).text |
| 120 | + |
| 121 | + // Check if name is empty |
| 122 | + if (newItemName.isEmpty) { |
| 123 | + self.alertError("Unable to save", msg: "Item name can't be blank.") |
| 124 | + } |
| 125 | + // Current Name equal New Name |
| 126 | + else if (currentName == newItemName) { |
| 127 | + true // do nothing |
| 128 | + } |
| 129 | + // Check duplicate item name |
| 130 | + else if (Item.checkDuplicate(newItemName, inManagedObjectContext: self.coreDataDB)) { |
| 131 | + self.alertError("Unable to save", msg: "There is already an item with name: \(newItemName).") |
| 132 | + } |
| 133 | + // Save CoreData |
| 134 | + else { |
| 135 | + // Update item name |
| 136 | + let item = Item.search(currentName, inManagedObjectContext: self.coreDataDB) |
| 137 | + item?.name = newItemName |
| 138 | + item?.save(self.coreDataDB) |
| 139 | + |
| 140 | + // Reload Coredata data |
| 141 | + self.items = Item.fetchAll(self.coreDataDB) |
| 142 | + |
| 143 | + // Reload TableView |
| 144 | + self.tableView.reloadData() |
| 145 | + } |
| 146 | + |
| 147 | + // Close cell buttons |
| 148 | + self.tableView.setEditing(false, animated: false) |
| 149 | + } |
| 150 | + |
| 151 | + let cancelButton = UIAlertAction(title: "Cancel", style: .Destructive, handler: nil) |
| 152 | + |
| 153 | + // Show textField in alert with current name value |
| 154 | + alertEdit.addTextFieldWithConfigurationHandler { textField in |
| 155 | + textField.text = currentName |
| 156 | + } |
| 157 | + |
| 158 | + alertEdit.addAction(cancelButton) |
| 159 | + alertEdit.addAction(saveButton) |
| 160 | + |
| 161 | + self.presentViewController(alertEdit, animated: true, completion: nil) |
| 162 | + } |
| 163 | + |
| 164 | + var deleteSwipteButton = UITableViewRowAction(style: .Normal, title: "Delete") { (action, indexPath) in |
| 165 | + // Find item |
| 166 | + let itemDelete = self.items[indexPath.row] |
| 167 | + |
| 168 | + // Delete item in CoreData |
| 169 | + itemDelete.destroy(self.coreDataDB) |
| 170 | + |
| 171 | + // Save item |
| 172 | + itemDelete.save(self.coreDataDB) |
22 | 173 |
|
| 174 | + // Tableview always load data from "items" array. |
| 175 | + // If you delete a item from CoreData you need reload array data. |
| 176 | + self.items = Item.fetchAll(self.coreDataDB) |
| 177 | + |
| 178 | + // Remove item from TableView |
| 179 | + self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic) |
| 180 | + } |
| 181 | + |
| 182 | + editSwipeButton.backgroundColor = UIColor.lightGrayColor() |
| 183 | + deleteSwipteButton.backgroundColor = UIColor.redColor() |
| 184 | + |
| 185 | + return [editSwipeButton, deleteSwipteButton] |
| 186 | + } |
| 187 | + |
| 188 | + // MARK: - Helpers |
| 189 | + |
| 190 | + /// Show a alert error |
| 191 | + /// |
| 192 | + /// :param: title Title |
| 193 | + /// :param: msg Message |
| 194 | + /// |
| 195 | + func alertError(title: String, msg: String) { |
| 196 | + var alert = UIAlertController(title: title, message: msg, preferredStyle: .Alert) |
| 197 | + let okButton = UIAlertAction(title: "OK", style: .Cancel, handler: nil) |
| 198 | + |
| 199 | + alert.addAction(okButton) |
| 200 | + |
| 201 | + self.presentViewController(alert, animated: true, completion: nil) |
| 202 | + } |
23 | 203 |
|
24 | 204 | } |
25 | 205 |
|
0 commit comments