Lapinou Lapinou - 4 months ago 53
iOS Question

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

}
}
}

Answer

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.