Guest User

Untitled

a guest
Jan 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. //
  2. // ViewController.swift
  3. // Todoey
  4. //
  5. // Created by Jessamine Briones on 16/1/19.
  6. // Copyright © 2019 Jessamine Briones. All rights reserved.
  7. //
  8.  
  9. import UIKit
  10.  
  11. class ToDoListViewController: UITableViewController {
  12.  
  13. var itemArray = [Item]()
  14.  
  15. let defaults = UserDefaults.standard
  16.  
  17. let dataFilePath = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first?.appendingPathComponent("Items.plist")
  18.  
  19. override func viewDidLoad() {
  20. super.viewDidLoad()
  21.  
  22. print (dataFilePath)
  23.  
  24. let newItem = Item()
  25. newItem.title = "Buy dumplings"
  26. itemArray.append(newItem)
  27.  
  28. let newItem2 = Item()
  29. newItem2.title = "Go to the beach"
  30. itemArray.append(newItem2)
  31.  
  32. let newItem3 = Item()
  33. newItem3.title = "Buy ice cream"
  34. itemArray.append(newItem3)
  35.  
  36.  
  37.  
  38. if let items = defaults.array(forKey: "ToDoListArray") as? [Item] {
  39. itemArray = items
  40. }
  41. // Do any additional setup after loading the view, typically from a nib.
  42.  
  43. }
  44.  
  45. override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  46. return itemArray.count
  47.  
  48.  
  49. }
  50.  
  51. override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  52.  
  53.  
  54. let cell = tableView.dequeueReusableCell(withIdentifier: "ToDoItemCell", for: indexPath)
  55.  
  56. let item = itemArray[indexPath.row]
  57.  
  58. cell.textLabel?.text = item.title
  59.  
  60. cell.accessoryType = item.done ? .checkmark : .none
  61.  
  62. return cell
  63. }
  64.  
  65. override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  66.  
  67.  
  68. itemArray[indexPath.row].done = !itemArray[indexPath.row].done
  69.  
  70. saveItems()
  71.  
  72.  
  73.  
  74. tableView.deselectRow(at: indexPath, animated: true)
  75.  
  76. }
  77.  
  78. //MARK - Add New Items
  79.  
  80. @IBAction func addButtonPressed(_ sender: UIBarButtonItem) {
  81.  
  82. var textField = UITextField()
  83.  
  84. let alert = UIAlertController(title: "Add New Todoey Item", message: "", preferredStyle: .alert)
  85. let action = UIAlertAction(title: "Add Item", style: .default) { (action) in
  86. // what happens once user clicks add button in ui alert
  87.  
  88. let newItem = Item()
  89. newItem.title = textField.text!
  90.  
  91. self.itemArray.append(newItem)
  92.  
  93. self.saveItems()
  94.  
  95.  
  96.  
  97. }
  98.  
  99.  
  100.  
  101. self.tableView.reloadData()
  102.  
  103. alert.addTextField { (alertTextField) in
  104. alertTextField.placeholder = "Create new item"
  105. textField = alertTextField
  106.  
  107. // extending the scope of the alertTextField by assigning var textField to it
  108. }
  109.  
  110. alert.addAction(action)
  111. present(alert, animated: true, completion: nil)
  112. }
  113.  
  114.  
  115. func saveItems() {
  116. let encoder = PropertyListEncoder()
  117.  
  118. do {
  119. let data = try encoder.encode(itemArray)
  120. try data.write(to: dataFilePath!)
  121. } catch {
  122.  
  123. print("Error encoding item array, \(error)")
  124.  
  125.  
  126. }
  127. }
  128.  
  129. }
Add Comment
Please, Sign In to add comment