In my to-do list app I'm using static cells to display an NSMutableArray of to-do items that never change. Once a person taps the cell an accessoryType of .Checkmark and is marked as completed. What I'm trying to do and haven't found an answer to yet is once every item in the array is completed and adds a checkmark a completionDate is returned for the entire list. The closest method I've found that can be called on NSArray or NSMutableArray is -removeAllObjects, but I don't want the Array destroyed. This is a daily checklist that needs to continue to be available. I would really appreciate someone pointing me in the right direction or even a better suggestion for how to do this. Thanks!
var checklistItems: NSMutableArray = []
override func viewDidLoad() {
super.viewDidLoad()
loadInitialData()
func loadInitialData(){
var item1 = Checklist(name: "Check Tires")
self.checklistItems.addObject(item1)
var item2 = Checklist(name: "Check Controls")
self.checklistItems.addObject(item2)
var item3 = Checklist(name: "Check Lights")
self.checklistItems.addObject(item3)
var item4 = Checklist(name: "Check Oil")
self.checklistItems.addObject(item4)
var item5 = Checklist(name: "Check Chassis")
self.checklistItems.addObject(item5)
var item6 = Checklist(name: "Check Sidestand")
self.checklistItems.addObject(item6)
}
override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
// #warning Potentially incomplete method implementation.
// Return the number of sections.
return 1
}
override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete method implementation.
// Return the number of rows in the section.
return self.checklistItems.count
}
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("ListPrototypeCell", forIndexPath: indexPath) as UITableViewCell
// Configure the cell...
var checklistItems: Checklist = self.checklistItems.objectAtIndex(indexPath.row) as Checklist
cell.textLabel?.text = checklistItems.itemName
if checklistItems.completed{
cell.accessoryType = .Checkmark
}
else{
cell.accessoryType = .None
}
return cell
}
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var tappedItem: Checklist = self.checklistItems.objectAtIndex(indexPath.row) as
Checklist
tappedItem.completed = !tappedItem.completed
tableView.reloadData()
}
I am not sure how you want to return a completion date from this list, perhaps via a delegate method or through an unwind segue, but I can suggest a method for determining whether the entire list is checked.
If you add an NSMutableSet
to hold the checked items you can quickly determine that all items are checked when the count of the checked item set is equal to the count of the array -
var checklistItems: NSMutableArray = []
var checkedItems = NSMutableSet()
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
tableView.deselectRowAtIndexPath(indexPath, animated: false)
var tappedItem: Checklist = self.checklistItems.objectAtIndex(indexPath.row) as
Checklist
tappedItem.completed = !tappedItem.completed
if (tappedItem.completed) {
checkedItems.addObject(tappedItem)
} else {
checkedItems.removeObject(tappedItem)
}
if (self.allChecked()) {
self.performSegueWithIdentifier("unwindFromChecklist", sender:self)
} else {
tableview.reloadData()
}
}
func allChecked() -> Bool {
return checkedItems.count == checklistItems.count
}
I have added an unwind segue call - You can simply grab the current date and time in your unwind method in the calling view controller or you could set the current date in a property of this view controller before invoking the unwind segue