I need to get an array of all the cells in my UITableView. I currently use the method below:
-(NSArray *)allTableViewCellsArray
{
NSMutableArray *cells = [[NSMutableArray alloc] init];
for (NSInteger j = 0; j < [tableView numberOfSections]; ++j)
{
for (NSInteger i = 0; i < [tableView numberOfRowsInSection:j]; ++i)
{
[cells addObject:[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:j]]];
}
}
return cells;
}
I’ve had some success with it, however I have come to discover it crashes when a cell isn’t visible. So how can I get an array of all the cells in my UITableView regardless as to whether they are visible or not?
Are you re-using table cells in your implementation? If so, i think you cant get all the UITableViewCell objects from your UITableView because of the cell re-use logic of UITableView.
Therefore you’d need to “disable” the cell re-use mechanics in your code.
This can be accomplished by not dequeueing (i.e. not using the
dequeueReusableCellWithIdentifiermethod anymore) your cells inside thecellForRowAtIndexPathmethod of your table view data source delegate and by passing nil for thereuseIdentifierproperty for the cell init method (initWithStyle:reuseIdentifier:).Then your
allTableViewCellsArraymethod could probably work!But i think you’re still not going to have any luck accomplishing this.
from the Apple docs for
[tableView cellForRowAtIndexPath:]: