Let’s say I have a UITableView which has multiple rows. I want to get all the UITableViewCells as an NSArray at a certain point of time. I have tried
[tableView visibleCells]
but there is a problem with this approach: I can not have all those cells which are not currently in the current screen. So I turned to another approach:
-(NSArray *)cellsForTableView:(UITableView *)tableView
{
NSInteger sections = tableView.numberOfSections;
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (int section = 0; section < sections; section++) {
NSInteger rows = [tableView numberOfRowsInSection:section];
for (int row = 0; row < rows; row++) {
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [self.tableView cellForRowAtIndexPath:indexPath]; // **here, for those cells not in current screen, cell is nil**
[cells addObject:cell];
}
}
return cells;
}
but seems that this approach still can not get those cells which are not displayed in the screen. Can anyone help me on this?
I don’t think thats possible, simply because the tableView doesn’t store all cells. It uses a caching mechanism which only stores some cells, that are not visible, for reuse.
Why do you need all cells? Maybe you can achieve, what you’re trying to do, otherwise.