Lapinou
Lapinou

Reputation: 1477

Select all cells in UITableView even invisible cell in iOS

I have a method selectAll to select all my cells in my UITableView. This method check a checkbox (UIButton). It's work very well just for the "visible" cells but not for the "invisible" cells!

Here my method:

- (IBAction)selectAll:(id)sender {

    for (NSInteger s = 0; s < self.tableView.numberOfSections; s++) {
        for (NSInteger r = 0; r < [self.tableView numberOfRowsInSection:s]; r++) {

            CustomCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:r inSection:s]];

            if(!cell.checkbox.selected){
                cell.checkbox.selected = !cell.checkbox.selected;
                cell.account.checked = cell.checkbox.selected;
            }

        }
    }   
}

Upvotes: 2

Views: 2124

Answers (3)

Simone Pistecchia
Simone Pistecchia

Reputation: 2832

From the documentation:

cellForRowAtIndexPath:

Return Value:

An object representing a cell of the table or nil if the cell is not visible or indexPath is out of range.

You can create an array that contains a list of booleans for checked or unchecked, and interrogate it when the cell is visible.

Upvotes: 4

Rahul
Rahul

Reputation: 134

Check it in cellForRowAtIndexPath. Just check the if condition with the suitable indexPath.section

if (indexPath.section== add the section){
cell.accessoryType = UITableViewCellAccessoryCheckmark;
}
else{
cell.accessoryType = UITableViewCellAccessoryNone;
}

Upvotes: 0

Michael Dautermann
Michael Dautermann

Reputation: 89509

You need to check or uncheck your "selected" or "checked" state of your cells in the "cellForRowAtIndexPath" method. The underlying data source is another place where you can keep track of what should be the state of the data you're trying to represent in the cells.

Simply modifying the UITableViewCells via this function is only going to update the cells that are currently visible within the table view.

Upvotes: 0

Related Questions