views:

82

answers:

3

Hey guys, I'd like to know the best strategy for adaptive cell heights. The cells first know how high they will be when they are created (they contain some textboxes and images) in the cellForRowAtIndexPath method. My idea was to store the cell's height in a NSMutuableDictionary with a cell identifiying key. The problem is that the heightForRowAtIndexPath method is called before the cells are created and only then.

How do you manage that usually?

Thanks!

+3  A: 

If you want to resize your cells (that is - to make heightForRowAtIndexPath method get called) you can use empty beginUpdates - endUpdates block:

[table beginUpdates];
[table endUpdates];

That will force UITableView to update its geometry and cells heights will be updated

Vladimir
A: 

Hello, If you want to adapt you cell's height, use - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath from the UITableViewDelegate. Store the cell's height into a NSArray for example and get it thanks to the indexPath parameter.

Kick
A: 

I do the following now: I calculate the height from the string height directly, I use:

-(CGFloat) estimateCellheightFromText:(NSString*) text andFont:(UIFont*)ffont widthConstraint:(CGFloat)width andLineBreakMode:(UILineBreakMode)lbMode{
CGSize stringSize = [text sizeWithFont:ffont constrainedToSize:CGSizeMake(width, 9999) lineBreakMode:lbMode]; return stringSize.height;}
Cocoprogrmr