Claus
Claus

Reputation: 5722

Find selected cells in a TableView

I'm writing an iPhone application with a tableview which displays a list of items,each one with a checkmark. I can selected/deselect each item just with one click. When I press a button [DONE] I would like to iterate along all the cells and check which one has the checkmark enabled. Something like:

    for (int i = 0; i < [fullDataset count]; i++) {
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:0];
    UITableViewCell* cell = [self.tableView cellForRowAtIndexPath:indexPath];
    if (cell.accessoryType == UITableViewCellAccessoryCheckmark) {
        NSLog(@"THIS CELL IS SELECTED!");
    } 
}

Now,the problem is that using this procedure I get only the cell which are selected and which are CURRENTLY DISPLAYED. I want to be sure that the procedure runs across ALL the cell,even the ones not displayed. I might use another data structure for keeping track of the selection but it looks to be a bit redundant.

Any idea?

Thanks a lot!

Claus

Upvotes: 1

Views: 179

Answers (1)

Eyal
Eyal

Reputation: 10818

You can't count on the cells to tell u if selected or not, cells get reused and always change there state.
You need to manage a separate NSSet of the selected indexPaths, when user tap on a cell, u add this indexPath to the set, when he deselect the cell u delete the indexPath from the set.

Upvotes: 2

Related Questions