Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Fabio Floris
Fabio Floris
526 Points

TableViewCell Auto Height on label text

Hello everyone, does anybody know how to set up a cell in a TableView with a variable height depending on the content of a label?

on the network and 'full of tutorials but for ios7 of 5 Xcode is deprecated and all the information I have very clear ... can you help?

P.S. I'm using Destruction APP Tutorial

1 Answer

Fabio Floris
Fabio Floris
526 Points

Now I can see everything perfectly ..! I should have resolved in this way:

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;
     NSString *comment = [[self.objects objectAtIndex:[indexPath row]] objectForKey:(@"Testo")];
     CGFloat whidt =  300;
     UIFont *FONT = [UIFont systemFontOfSize:11];
     NSAttributedString *attributedText =[[NSAttributedString alloc]  initWithString:comment  attributes:@  {      NSFontAttributeName: FONT }];
      CGRect rect = [attributedText boundingRectWithSize:(CGSize){whidt, MAXFLOAT}
     CGSize size = rect.size;
      return size.height +50; 

 - (FFCustomCellTimeline *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object {

     FFCustomCellTimeline *cell = (FFCustomCellTimeline * )[self.tableView     dequeueReusableCellWithIdentifier:@"Cell"];
      if (!cell) {
          cell = [[FFCustomCellTimeline alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];

       NSString *text = [object objectForKey:(@"Testo")];
      CGSize constraint = CGSizeMake(300 , 20000.0f);
      UIFont *FONT = [UIFont systemFontOfSize:11];
       CGSize size = [text boundingRectWithSize:constraint options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:FONT }context:nil].size;

       cell.FFNomeLabel.frame = CGRectMake(10, 0, 300, MAX(size.height, 54.0f) + 20.0f);
       cell.FFNomeLabel.numberOfLines = 0;
       cell.FFNomeLabel.lineBreakMode = NSLineBreakByWordWrapping;
       cell.FFNomeLabel.text = text;
       cell.FFNomeLabel.font = [UIFont systemFontOfSize:11];

       return cell;