views:

346

answers:

2

I have a UITableView of custom UITableViewCells which looks like this:

[ CELL 0
    [ description ]
    [ dynamic content type 1 ]
    [ dynamic content type 2 ]
    [ dynamic content type 3 ]
 ]
 [ CELL 1
    [ description ]
    [ dynamic content type 1 ]
    [ dynamic content type 3 ]
 ]
 [ CELL 2
    [ description ]
    [ dynamic content type 2 ]
 ]
 [ ... and so on ... ]

Since the [description] part is already pretty complex I decided to use Interface Builder to design it and at add the [dynamic content] in the cellForRowAtIndexPath programmatically with [cell addSubview:...]. My problem is now, that I set a default height for my custom UITableViewCell in Interface Builder, but when I add my [dynamic content] (which might range between 0..3) I have different cells with different heights.

One thing is of course to calculate the total height and change the return value in heightForRowAtIndexPath, but how do I change the height value of my actual cell (which was loaded from a nib file with a fixed height)?

+2  A: 

Why don't you update the bounds of the cell in cellForRowAtIndexPath (where you already add the dynamic contents)?

Nikolai Ruhe
The TableView Widget will allocate the height for every cell before it gets the cell. This is to determine the scroll length and some other performance considerations I imagine. If you just set the bounds, the height will work for one but then it will step on top of the rest of the cells.
Brian King
@Brian I think that's what Stefan meant in his last sentence. He's already calculating the correct height and returning it in the delegate method `heightForRowAtIndexPath`. He's concerned about the cell's bounds. I'm not sure if the UITableView modifies the size of the cell. It does modify the frame’s origin in `-[UITableView layoutSubviews]`because it has to arrange the cells.
Nikolai Ruhe
I actually tried it with setFrame before, but didn't work. I probably messed something else up at the same time. I now tried it with setFrame and setBounds and both works :-) Muchas gracias.
znq
Hah, yeah, apparently my reading skills were a little fuzzy this AM!
Brian King
De nada. (And it's already Friday.)
Nikolai Ruhe
A: 

You want to compute and return the value in:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

One thing to note is that this will be called for every row in your view on startup, so be careful on what you create to compute the size.

The other option, which may be more standard in implementation is to do a grouped TableView widget and have what you call a cell here a section, and then every row either a description or dynamic content. That's how I'd do it if your ascii drawing was my design spec, but I'm guessing you have more things you're running off of.

Brian King