PDA

View Full Version : [iPhone] Array of Cells




Duke Leto
Jul 8, 2008, 05:50 PM
How do you get an array of all of the cells in a tableView?



lucasgladding
Jul 8, 2008, 06:39 PM
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).


- (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.

Duke Leto
Jul 8, 2008, 09:08 PM
Hmmm.. I never heard about categories..

And thanks for the code!

lucasgladding
Jul 8, 2008, 09:46 PM
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:


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


and


@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.

mputnamtennesse
Oct 12, 2011, 11:07 AM
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

North Bronson
Oct 12, 2011, 12:19 PM
One small tip is to prefix all of your category methods. For example:

- (NSArray *)allCells

becomes:

- (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.

Sydde
Oct 12, 2011, 08:30 PM
One small tip is to prefix all of your category methods. For example:

- (NSArray *)allCells

becomes:

- (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).

idelovski
Oct 13, 2011, 01:18 PM
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).

admanimal
Oct 13, 2011, 02:28 PM
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