iPad: Iterate over every cell in a UITableView?
7 Answers
for (int section = 0; section < [tableView numberOfSections]; section++) {
for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) {
NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell* cell = [tableView cellForRowAtIndexPath:cellPath];
//do stuff with 'cell'
}
}
-
6It is normal that it only works on visible cells. Invisible cells are dequeued from the memory; cellForRowAtIndexPath will return nil for them. Sep 24, 2013 at 11:08
-
@RaviSharma add [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO]; and it will be used in non-visible cells.– zaolianJan 25, 2016 at 20:33
-
1@zaolian - think about what your suggestion does. Its a great example of why this whole approach is misguided: you are forcing the tableview to load every row from the source. If the needed info is in the source, then that is a better place to get it. If the question-asker was trying to read back changes from what is on screen, then your suggestion will seem to work - until user makes a change far enough down in a table, that scrolling back to the first row causes the changed row to be discarded - and lost forever - before this logic reads it. Apr 7, 2017 at 4:02
To iterate over every visible cell in a UITableView:
for (UITableViewCell *cell in self.tableView.visibleCells) {
NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
(edited to better state answer and hope that this is indexed more accurately for search results with the intention of saving others more time in the future)
-
3That's just for visible cells.– user244343Apr 13, 2011 at 5:10
-
I should've stated that - I updated my answer to reflect your comment.– AndrewPKSep 13, 2011 at 15:09
-
+1 for using an oop style instead of a function approach, even though you would'nt need to do this unless you're updating the GUI style. Jan 22, 2013 at 13:58
(This builds on aroths answer.)
I like to define this as a category to UITableView
so it's available everywhere.
(As mentioned a few times, you should be sure you really want to iterate over the cells themselves. For example: I use this to clear the UITableViewAccessoryCheckmark
's from all the cells before setting it to the user selected cell. A good rule of thumb is to do this only if the datasource methods can't do what you need to.)
Define like this:
- (void)enumerateCellsUsingBlock:(void (^)(UITableViewCell *cell))cellBlock {
NSParameterAssert(cellBlock != nil);
for (int section = 0; section < [self numberOfSections]; section++) {
for (int row = 0; row < [self numberOfRowsInSection:section]; row++) {
NSIndexPath *cellPath = [NSIndexPath indexPathForRow:row inSection:section];
UITableViewCell *cell = [self cellForRowAtIndexPath:cellPath];
if (cellBlock != nil) {
cellBlock(cell);
}
}
}
}
Call like this:
[self.tableView enumerateCellsUsingBlock:^(UITableViewCell *cell) {
NSLog(@"cell:%@", cell);
}];
It would be good style to typedef the block, too.
Swift 4:
for section in 0...self.tableView.numberOfSections - 1 {
for row in 0...self.tableView.numberOfRows(inSection: section) - 1 {
let cell = self.tableView.cellForRow(at: NSIndexPath(row: row, section: section) as IndexPath)
print("Section: \(section) Row: \(row)")
}
}
by steve Iterate over all the UITableCells given a section id
Assuming a variable myTableView
exists and its delegate and data source are both set:
UITableViewCell *cell;
NSIndexPath indexPath = [[NSIndexPath indexPathForRow:0 inSection:0];
for(indexPath.section = 0; indexPath.section < [myTableView numberOfSections]; ++indexPath.section)
{
for(indexPath.row = 0; indexPath.row < [myTableView numberOfRowsInSection:indexPath.section]; ++indexPath.row)
{
cell = [myTableView cellForRowAtIndexPath:indexPath];
// do something with this cell
}
}
-
Getting these errors for indexPath.row..... 1. Assignment to read only property 2. Increment to read only property....– iSeekerFeb 21, 2014 at 6:01
-
You mustn't have created a new
NSIndexPath
local variable. Check indexPath isn't the one being passed into your tableview data source function or something like that.– user244343Feb 26, 2014 at 1:03
Even simpler and more elegant:
-(void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
// use the "cell" here
}
But obviously it doesn't fit all situations.
This how Im iterating over all table view cells even not visible ones , check my answer here :
https://stackoverflow.com/a/32626614/2715840
Hint : code in Swift.
-
Your solution in OC version, just for reference: [self.tableView scrollToRowAtIndexPath:indexPath atScrollPosition:UITableViewScrollPositionTop animated:NO];– zaolianJan 25, 2016 at 20:33
cellForRowAtIndexPath:
you can set acell
property of your underlying data structure to point to the initialised cell. Make sure it's an@property (assign)
though, not aretain
, since you don't want to keep all cells in memory for the sake of reuse.editDidEnd
or similar method. Then when "Done", you are examining your custom model data - the displayed fields are not needed.