0

I have a table view with custom cells. I'm using Xcode 5.1 and iOS 7.1. I would like to adjust a UILabel height based on the length of a string and also dynamically adjust the height of the cell to fit the label.

In

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

    cell.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
    cell.titleLabel.numberOfLines = 0;
    [cell.titleLabel sizeToFit];

Some seem to resize correctly, others don't. As for the height of the cell, I can't get it to update based on the height of the label.

Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
  • see this http://stackoverflow.com/questions/19215199/dynamically-size-uitableviewcell-according-to-uilabel-with-paragraph-spacing/19217965#19217965 – Shankar BS Apr 15 '14 at 05:51

2 Answers2

0

The cell's height is determined solely by the value you return from tableView:heightForRowAtIndexPath:. This is a <UITableViewDelegate> method.

If you want to have the cell height be dynamic, you would need to perform a calculation in tableView:heightForRowAtIndexPath: based on the particular string that appears at that index path.

If you plan on doing a lot of scrolling, this calculation could get expensive and cause performance issues. If that is the case, I'd recommend caching the computed heights or computing them all at once and storing them in an array parallel with your data source.

Nick
  • 2,361
  • 16
  • 27
0
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

     return [self dynamicHeightAtIndexPath:indexPath]+20;

}


-(CGFloat)dynamicHeightAtIndexPath:(NSIndexPath *)indexPath
{
   CGSize maximumSize = CGSizeMake(275, 9999);
   UIFont *myFont = [UIFont fontWithName:@"MyriadPro" size:11];
   CGSize stringsize = [[self.array objectAtIndex:indexPath.row] sizeWithFont:myFont
                                                         constrainedToSize:maximumSize
                                                             lineBreakMode:NSLineBreakByWordWrapping];
   return stringsize.height;

}
Mayank Jain
  • 5,663
  • 7
  • 32
  • 65
  • Awesome! This worked perfectly for me with resizing the table view cells. I am still running into an issue with the label itself though.Even though the label extends to all the way from the left to the right, sometimes the text either ends up like: Text Text Text Text (one on top of the other) Instead of: text text text text (in one line extending the whole cell) or you click on the cell, push a view controller, and come back and it appears as it should but as you scroll, you see some that are messed up. – lizbarrett Apr 16 '14 at 17:11
  • please provide screenshot if you can – Mayank Jain Apr 17 '14 at 05:24
  • I have uploaded the photos into a folder on Dropbox - https://www.dropbox.com/sh/4016fxw8qfutpoq/jtVwMw8zQs If you need more, just let me know. Some strings seem to be calculating correctly but truncating - others are working but stacking weirdly. – lizbarrett Apr 17 '14 at 14:59
  • It also seems that when you see the messed up labels, click the cell, push the new VC, and go back - the ones that were messed up, seem to be fixed. – lizbarrett Apr 17 '14 at 15:01