Jesper Martensson
Jesper Martensson

Reputation: 1248

Customizing UITableViewCell's height

How can I scale UITableViewCells based on the amount of content in them? In my cells I use 3 labels which represent a forum. The labels are named "alias", "date", and "comments". The third label comments, can be any number of size. Therefore, my cells need to become dynamically depending on how big the "comments" label is. Below is my relevant code:

- (UITableViewCell *)tableView:(UITableView *)pTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"ForumthreadCell";
UITableViewCell *cell = [pTableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell == nil)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}

Feedback *item = [self.items objectAtIndex:indexPath.row];

UILabel *aliasLabel = (UILabel *)[cell viewWithTag:1];
UILabel *commentLabel = (UILabel *)[cell viewWithTag:2];
UILabel *dateLabel = (UILabel *)[cell viewWithTag:3];

[aliasLabel setText:item.alias];
[commentLabel setText:item.comment];
[dateLabel setText:[self.dateFormatter stringFromDate:[NSDate    dateWithTimeIntervalSince1970:(double)item.time]]];

commentLabel.numberOfLines = 0;
[commentLabel sizeToFit];

CGSize textHeight = [commentLabel.text sizeWithFont:commentLabel.font  constrainedToSize:CGSizeMake(maxWidth, lineHeight *    commentLabel.numberOfLines)lineBreakMode:UILineBreakModeWordWrap];

return cell;
}

As you can see, I set the height of the commentsLabel with textHeight. But what shall I do from now on? If I know the height of the label, how can I set the Cell's height depending on commentLabel's height?. Please give code-examples based on my code. I think I should use the following method but I am not sure how to do it:

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

}

Upvotes: 0

Views: 1024

Answers (2)

Sean Lintern
Sean Lintern

Reputation: 3141

You could make the Label declarations in your implementation:

@implementation pTableView
{
    UILabel *aliasLabel;
}

Then in the row height you could use default Height (44) + aliasLabel.Height

return 44 + aliasLabel.frame.size.height;

Then if you need to differentiate between the labels in your table

aliasLabel.tag = indexpath.row

Upvotes: 0

Guru
Guru

Reputation: 5393

Use this method to get the text height of the text

-(CGFloat)getLabelHeightForText:(NSString *)text andWidth:(CGFloat)labelWidth
{

CGSize maximumSize = CGSizeMake(labelWidth, 10000);

//provide appropriate font and font size
CGSize labelHeighSize = [text sizeWithFont: [UIFont fontWithName:@"Trebuchet MS" size:13.0f]
                         constrainedToSize:maximumSize
                             lineBreakMode:UILineBreakModeTailTruncation];
return labelHeighSize.height;
}

This method will return the height of the text you are passing. Add this method in your class. And use the tableView:heightForRowAtIndexPath: delegate method to set the height for each cell

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   Feedback *item = [self.items objectAtIndex:indexPath.row];

   CGFloat textHeight = [self getLabelHeightForText:item.comment andWidth:162];//give your label width here
    return textHeight;
}    

I think this could solve the problem.

Upvotes: 6

Related Questions