Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.
You could add this in a category for UITableView. If you don't want to use a category, just replace self with tableView (or the variable name you use for your UITableView instance).

Code:
- (NSArray *)allCells
{
     NSMutableArray *cells = [NSMutableArray array];
     NSInteger i, j;
     NSInteger sections = [self numberOfSections];
     for (i = 0; i < sections; i++)
     {
          NSInteger rows = [self numberOfRowsInSection:i];
          for (j = 0; j < rows; j++)
          {
               NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
               UITableViewCell *cell = [self cellForRowAtIndexPath:indexPath];
               [cells addObject:cell];
          }
     }
     return cells;
}

If anyone new to programming is reading this: take the time to learn how to use categories. When I started programming, I subclassed a lot of things that could have been handled with much less work using categories.
 
Hmmm.. I never heard about categories..

And thanks for the code!

No problem.

There's nothing really difficult about categories. The main thing to know IMHO are that you can't add instance variables like you would with subclasses. If you're just adding methods to existing functionality for convenience, however, they are an excellent option. Simply add the .h file for the category to any class that needs the extensions. Details for the previous example would typically use UITableView+DLAdditions.h and .m as filenames and contain:

Code:
@interface UITableView (DLAdditions)
- (NSArray *)allCells;
@end

and

Code:
@implementation UITableView (DLAdditions)
- (NSArray *)allCells
{
...
}
@end

Just call [tableView allCells] on any UITableView instance and you should get the cells array back.

I know the 2nd edition of the Aaron Hillegass has a chapter on categories and I assume the 3rd edition will include them as well.
 
A couple of errors

This is great stuff... an especially useful intro to categories.
However, there is an error in the code snippet.

NSIndexPath *indexPath = [NSIndexPath indexPathForRow:i inSection:j];
should be
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:j inSection:i];

as the rows are counted with j and the sections with i.

Thanx
 
One small tip is to prefix all of your category methods. For example:

Code:
- (NSArray *)allCells

becomes:

Code:
- (NSArray *)NBS_allCells

The reason is that if Apple decides to implement their own allCells method in a future version of the OS, wacky things can happen. You want to prefix your methods with a unique string (your name or your company) that you know Apple won't be using for their own work.
 
One small tip is to prefix all of your category methods. For example:

Code:
- (NSArray *)allCells

becomes:

Code:
- (NSArray *)NBS_allCells

The reason is that if Apple decides to implement their own allCells method in a future version of the OS, wacky things can happen. You want to prefix your methods with a unique string (your name or your company) that you know Apple won't be using for their own work.

Also be aware that if you code an existing method name, that will replace the factory method completely (your method cannot call super the way a subclass can or otherwise access the factory method).
 
I am not sure the code in post #2 would work. There is this comment in UITableView.h near the cellForRowAtIndexPath:

// returns nil if cell is not visible or index path is out of range

Maybe you should check it in real project (and even that wouldn't prove anything in the light of the remark from the header file).
 
I am not sure the code in post #2 would work. There is this comment in UITableView.h near the cellForRowAtIndexPath:

// returns nil if cell is not visible or index path is out of range

Maybe you should check it in real project (and even that wouldn't prove anything in the light of the remark from the header file).

I'm guessing Duke has figured it out by now, since the original question was asked in 2008. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.