How to make TableViewCell of different sizes?

Discussion in 'iOS Development' started by cybohemia, Mar 13, 2009.

  1. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    I'm trying to make a table where each cell could be one of two sizes but I can't seem to get the size of the cells to change even when I set their bounds.size to the desired dimensions.

    In the code snippet below, I try to make the second row (row==1) of a greater height and I put a TextView inside:

    Code:
    if (indexPath.row == 1) {
      if (cell == nil) { 
        CGRect  cellViewFrame = CGRectMake(20, 20, 250, 200);			
        CGRect textViewFrame = CGRectMake(70, 10, 150, 100);
    
        cell = [[[UITableViewCell alloc] initWithFrame:cellViewFrame reuseIdentifier:CellID] autorelease];  // CellID is @"Cell"
        UITextView *text = [[UITextView alloc] initWithFrame:textViewFrame];
    
      ...etc...
    
    But, the result isn't what I expected - row==1 seems to extend to the given height but, as the screen capture shows, row==2 comes in as if the previous row had the default size.

    Please Register or Log in to view images




    I'm using
    Code:
    CGRectZero
    to create the cell for row==2. I'm hoping I don't have to book-keep the starting position for all the subsequent cells...or is that the way I should do it? Is there something I'm missing?

    Thanks for any ideas!

    Thanks!
  2. Steaps

    Steaps New Member

    Joined:
    Oct 24, 2007
    Messages:
    5,074
    Likes Received:
    41
    Device:
    iPod touch
    I think you could use this:
    Code:
    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
         if (indexPath.row == 1) {
              return cellOneHeight;
         } else if (indexPath.row == 2) {
              return cellTwoHeight;
         }
    }
  3. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    Go ahead and subclass your cells, it's not as complex and challenging as everyone seems to think it is. Then alternate the type of cell.

    Code:
    /* In the cellForRow method */
    return (indexPath.row % 2 == 0 ? aCell : anotherCell);
    

    EDIT: Clearly, you will need to create two reusable cells instead of the one included in the sample code Apple provides, that's no big deal at all. When initializing the cell, set its height. Subclassing its drawRect message is also fun!
  4. cybohemia

    cybohemia New Member

    Joined:
    Jan 25, 2009
    Messages:
    49
    Likes Received:
    0
    Device:
    iPhone 3G (Black)
    Thanks, Stephen.4 & SkylarEC! They both solved the problem - I like the heightForRowAtIndexPath approach because it uses the SDK's method.

    But subclassing would be an elegant way, too, although I can't seem to shake a thing I remember from the J2ME world that said something about minimizing some OO structure for optimization purposes(!). Kinda bad old school stuff I should toss out, I know

    Please Register or Log in to view images

  5. SkylarEC

    SkylarEC Super Moderator Emeritus Staff Member

    Joined:
    Sep 19, 2007
    Messages:
    6,642
    Likes Received:
    129
    both suggestions are SDK compliant.

    Remember that the only one cell "exists" at a time. Two cells "existing" isn't all that much more than one, if you catch my drift. Also, the only cells that are actually existent are the cells physically on the screen (plus a couple more).

    HeightForRowAtIndexPath fails when you don't know which cells are going to be which height ahead of time, or if you need one type of cell, then another. Also, it fails when you don't know the height of the cell ahead of time.

    Although it is quick and easy, it isn't flexible in the least.



    EDIT: Plus, you can't do cool things like this without subclassing the UITableCells. http://www.ifans.com/forums/showthread.php?t=137518 . Download the app linked within that link and run it. Everything it does it not only SDK compliant, it all also works on the simulator!
    : )
    (The categories section/button won't show up if you don't have Categories installed).

Share This Page