Search code examples
iosuitableviewnsindexpath

Easier way to select all rows in UITableView


I wish to select all rows in UITableView for which I am using a for loop as per below code:

- (void)selectAllRows
{
    for (int row = 0; row < [self.tableView numberOfRowsInSection:1]; row ++)
    {
        NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:1];
        [self.tableView selectRowAtIndexPath:indexPath animated:NO scrollPosition:UITableViewScrollPositionNone];
    }
}

I couldn't find any direct delegates on NSIndexPath class or on UITableView controller to select all the rows on UITableView cell. This is of 'n' complexity, but could there be a better way to select all the cells?


Solution

  • This is the only way to select all the cells. The real question is why are you wanting to select all the cells? Are you selecting them to get UITableView delegate callbacks? Are you selecting them for UI purposes?