Question

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;
            }

        }
    }   
}
Was it helpful?

Solution

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.

OTHER TIPS

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.

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;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top