Cocoprogrmr
Cocoprogrmr

Reputation: 31

UITableView: Adaptive cell height

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 NSMutableDictionary 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?

Upvotes: 2

Views: 1392

Answers (3)

Cocoprogrmr
Cocoprogrmr

Reputation: 31

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;}

Upvotes: 0

Damien Marzlin
Damien Marzlin

Reputation: 39

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.

Upvotes: 0

Vladimir
Vladimir

Reputation: 170839

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

Upvotes: 6

Related Questions