Вопрос

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?

Это было полезно?

Решение

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?

Другие советы

You can't reference all cells in your tableView as the tableView doesn't "have" all of them as it uses reuse/dequeueing.

If by "UI purposes" you mean solely for the visual effect as opposed to, you could loop through all visible cells in your tableview and set the alpha to 0.5 or something similar, and if you want to fade them out and in as if they were selected/deselected you could do some [UIView animate....] calls on all of them. If you were to do something like this, you might want to consider disabling scrolling for the short period of time your animation is occurring, because if you are doing animation on visible cells as soon as you scroll any of the new cells wouldn't have that animation. Once you're done animating, in the completion block, you can then re-enable scrolling.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top