Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 1.49 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. iPad: Iterate over every cell in a UITableView?
  2. for (int section = 0; section < [tableView numberOfSections]; section++) {
  3.     for (int row = 0; row < [tableView numberOfRowsInSection:section]; row++) {
  4.         NSIndexPath* cellPath = [NSIndexPath indexPathForRow:row inSection:section];
  5.         UITableViewCell* cell = [tableView cellForRowAtIndexPath:cellPath];
  6.         //do stuff with 'cell'
  7.     }
  8. }
  9.        
  10. for (UITableViewCell *cell in self.tableView.visibleCells) {
  11.     NSIndexPath *cellIndexPath = [self.tableView indexPathForCell:cell];
  12.        
  13. UITableViewCell *cell;
  14. NSIndexPath indexPath = [[NSIndexPath indexPathForRow:0 inSection:0];
  15. for(indexPath.section = 0; indexPath.section < [myTableView numberOfSections]; ++indexPath.section)
  16. {
  17.     for(indexPath.row = 0; indexPath.row < [myTableView numberOfRowsInSection:indexPath.section]; ++indexPath.row)
  18.     {
  19.         cell = [myTableView cellForRowAtIndexPath:indexPath];
  20.         // do something with this cell
  21.     }
  22. }
  23.        
  24. - (void)enumerateCellsUsingBlock:(void (^)(UITableViewCell *cell))cellBlock {
  25.     for (int section = 0; section < [self numberOfSections]; section++) {
  26.         for (int row = 0; row < [self numberOfRowsInSection:section]; row++) {
  27.             NSIndexPath *cellPath = [NSIndexPath indexPathForRow:row inSection:section];
  28.             UITableViewCell *cell = [self cellForRowAtIndexPath:cellPath];
  29.             cellBlock(cell);
  30.         }
  31.     }
  32. }
  33.        
  34. [self.tableView enumerateCellsUsingBlock:^(UITableViewCell *cell) {
  35.     NSLog(@"cell:%@", cell);
  36. }];