Search code examples
iphoneiosuitableviewnsindexpath

Checking and unchecking UITableViewCells with a click of a UIToolBarButton


How can you put a checkmark beside all the cells and remove the checkmark with a click of a UIToolBarButton? Is this possible?

I have tried to iterate through the number of cells loading in the tableview but I don't know how to set the accessorytype to checkmark for all of them and then back to none when all of them are checked?


Solution

  • Don't change the cells directly -- change the data that's used to populate the cells, and then tell the table to reload it's data.

    So, you'll need the following:

    • a way to store represent the 'checked state' in the data that the cells represent;

    • a way to set the accessory for a single cell in your -tableView:cellForRowAtIndexPath: method according to the value of the 'checked state' for the item that the cell in question represents

    For a normal UITableViewCell, you can set the accessory by saying something like:

    cell.accessoryType = item.isChecked ? UITableViewAccessoryCheckmark : UITableViewAccessoryNone;