[iPhone] Array of Cells

Discussion in 'iOS Programming' started by Duke Leto, Jul 8, 2008.

  1. macrumors regular

    Joined:
    Mar 17, 2008
    #1
    How do you get an array of all of the cells in a tableView?
     
  2. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #2
    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.
     
  3. thread starter macrumors regular

    Joined:
    Mar 17, 2008
    #3
    Hmmm.. I never heard about categories..

    And thanks for the code!
     
  4. macrumors 6502

    Joined:
    Feb 16, 2007
    Location:
    Waterloo, Ontario
    #4
    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.
     
  5. macrumors newbie

    Joined:
    Oct 12, 2011
    #5
    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
     
  6. macrumors 6502

    Joined:
    Oct 31, 2007
    Location:
    San José
    #6
    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.
     
  7. macrumors 68000

    Sydde

    Joined:
    Aug 17, 2009
    #7
    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).
     
  8. macrumors regular

    Joined:
    Sep 11, 2008
    #8
    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).
     
  9. macrumors 68040

    Joined:
    Apr 22, 2005
    #9
    I'm guessing Duke has figured it out by now, since the original question was asked in 2008. :D
     

Share This Page