subodhbahl
subodhbahl

Reputation: 415

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?

Upvotes: 0

Views: 1503

Answers (2)

malhal
malhal

Reputation: 30727

try this:

    for(UITableViewCell* cell in tableAlert.tableView.visibleCells){
        cell.accessoryType = UITableViewCellAccessoryChecked;
    }

You also need to update your data set so that when you scroll the cells get the correct accessory in cellForRow.

Upvotes: 0

Caleb
Caleb

Reputation: 125007

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;

Upvotes: 2

Related Questions