-
Notifications
You must be signed in to change notification settings - Fork 0
/
AllListsViewController.swift
129 lines (97 loc) · 4.52 KB
/
AllListsViewController.swift
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import UIKit
class AllListsViewController: UITableViewController, ListDetailViewControllerDelegate, UINavigationControllerDelegate {
var dataModel: DataModel!
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
tableView.reloadData()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
navigationController?.delegate = self
let index = dataModel.indexOfSelectedChecklist
if index != -1 && index < dataModel.lists.count {
let checklist = dataModel.lists[index]
performSegueWithIdentifier("ShowChecklist", sender: checklist)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return dataModel.lists.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = cellForTableView(tableView)
let checklist = dataModel.lists[indexPath.row]
cell.textLabel!.text = checklist.name
cell.accessoryType = .DetailDisclosureButton
let uncheckedItems = checklist.countUncheckedItems()
if checklist.items.count == 0 {
cell.detailTextLabel!.text = "(No Items)"
} else if uncheckedItems == 0 {
cell.detailTextLabel!.text = "All Done"
} else {
cell.detailTextLabel!.text = "\(checklist.countUncheckedItems()) Remaining"
}
cell.imageView!.image = UIImage(named: checklist.iconName)
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
dataModel.indexOfSelectedChecklist = indexPath.row
let checklist = dataModel.lists[indexPath.row]
performSegueWithIdentifier("ShowChecklist", sender: checklist)
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "ShowChecklist" {
let controller = segue.destinationViewController as! ChecklistViewController
controller.checklist = sender as! Checklist
} else if segue.identifier == "AddChecklist" {
let navigationController = segue.destinationViewController as! UINavigationController
let controller = navigationController.topViewController as! ListDetailViewController
controller.delegate = self
controller.checklistToEdit = nil
}
}
func cellForTableView(tableView: UITableView) -> UITableViewCell {
let cellIdentifier = "Cell"
if let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier) {
return cell
} else {
return UITableViewCell(style: .Subtitle, reuseIdentifier: cellIdentifier)
}
}
func listDetailViewControllerDidCancel(controller: ListDetailViewController) {
dismissViewControllerAnimated(true, completion: nil)
}
func listDetailViewController(controller: ListDetailViewController, didFinishAddingChecklist checklist: Checklist) {
dataModel.lists.append(checklist)
dataModel.sortChecklists()
tableView.reloadData()
dismissViewControllerAnimated(true, completion: nil)
}
func listDetailViewController(controller: ListDetailViewController, didFinishEditingChecklist checklist: Checklist) {
dataModel.sortChecklists()
tableView.reloadData()
dismissViewControllerAnimated(true, completion: nil)
}
override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
dataModel.lists.removeAtIndex(indexPath.row)
tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
override func tableView(tableView: UITableView, accessoryButtonTappedForRowWithIndexPath indexPath: NSIndexPath) {
let navigationController = storyboard!.instantiateViewControllerWithIdentifier("ListDetailNavigationController") as! UINavigationController
let controller = navigationController.topViewController as! ListDetailViewController
controller.delegate = self
let checklist = dataModel.lists[indexPath.row]
controller.checklistToEdit = checklist
presentViewController(navigationController, animated: true, completion: nil)
}
func navigationController(navigationController: UINavigationController, willShowViewController viewController: UIViewController, animated: Bool) {
if viewController === self {
dataModel.indexOfSelectedChecklist = -1
}
}
}