Tim
Tim

Reputation: 13258

iPhone -> dynamic cell height

I have a UITableView and a Cell which I design with Interface Builder. I have two labels there. The problem is, that one of the labels is text which can be 500 words or 10 words.

So I need a dynamic cell height. How can I handle this, because otherwise I have empty space or the cell height is not enough.

Perhaps someone has an idea how to do this?

Best Regards.

Upvotes: 4

Views: 2890

Answers (2)

Ratinho
Ratinho

Reputation: 298

-(CGFloat)tableView:(UITableVIew*) tableView heightForRowAtIndexPath(NSIndexPath *) indexPath
{
   //here u can check the row number and ur labels and return ur custom height.
}

Upvotes: 2

Ludovic Landry
Ludovic Landry

Reputation: 11774

To know the size of your text, you can use this method:

- (CGFloat)measureTextHeight:(NSString*)text fontName:(NSString*)fontName fontSize:(CGFloat)fontSize constrainedToSize:(CGSize)constrainedToSize {
CGSize mTempSize = [text sizeWithFont:[UIFont fontWithName:fontName size:fontSize] constrainedToSize:constrainedToSize lineBreakMode:UILineBreakModeWordWrap];
return mTempSize.height; }

Then you can set your size cell with:

-(CGFloat)tableView:(UITableVIew*)tableView heightForRowAtIndexPath(NSIndexPath *)indexPath;

Upvotes: 5

Related Questions