views:

281

answers:

2

I have a checklist in a UITableView and I have a UISegmentedControl with "Select All" and "Deselect All" options.

I am wondering how I can reset all the cells while viewing the table. The [self.tableView reloadData]; function does not seem to do the trick.

Any thoughts?

Thanks!

+3  A: 

You could keep a NSMutableArray* called checkmarks that stores NSNumber instances.

Set +numberWithBool: on each element of checkmarks as YES, for example, to mark all rows as checked.

Note that this is just a data model store. Thus, you have to actually read the values of checkmarks in your -tableView:cellForRowAtIndexPath: method to set the checkmark states for each row's accessoryType, e.g.:

cell.accessoryType = ([[checkmarks objectAtIndex:indexPath.row] boolValue] == YES) ? UITableViewCellAccessoryCheckmark : UITableViewCellAccessoryNone;

When you programmatically change the state of checkmarks, you can then call [tableView reloadData] so that the state of table view rows reflects the state of elements of checkmarks.

Alex Reynolds
Thanks Alex. It's a little tricker in my situation because I have a sectioned tableView so I can't just call indexPath.row. However, just using indexPath doesn't seem to work either. Any thoughts?
Jonah
You need to come up with an equation that maps `indexPath.section` and `indexPath.row` to an index in your array. Or, keep an array of arrays, the top-level array for each section, and an inner array for each section's rows.
Alex Reynolds
+3  A: 

reloadData will just set up table cells with what you return from tableView:cellForRowAtIndexPath:. If you return a checked cell, it'll show up checked. UITableView doesn't maintain any sort of "checked" or "unchecked" state itself.

It does have "selected" and "unselected" states, but, according to Apple's HIG, you shouldn't be using that to indicate a selection. Those states are for the highlight you get when you've tapped a row, and only one row of a table should ever be "selected". Instead, you should use the accessory view or some custom subview of the cell to display a checkmark.

After you dequeue a table cell in tableView:cellForRowAtIndexPath:, make sure to explicitly set the accessory view to either UITableViewCellAccessoryCheckmark or UITableViewCellAccessoryNone. Otherwise, it will have whatever accessory view it had when it was last displayed.

Dustin Voss
It seems like my problem then is that the "tableView reloadData" is not calling. I've got it set up with an "if" statement in the "cellForRowAtIndexPath" method that checks if the "checked" or "unchecked" button is pressed. But when I press it, it doesn't reload the data.
Jonah
Don't check the button itself. Save your cells' checkmark status in an array like the Reynolds said. In the buttons' action method, set the array's values and then call `reloadData`.
Dustin Voss