0

The user can make a comment which can be can be any size or height. The comment label's dynamic content should fit into the cell.

I have allocated my label inside UITableViewCell with a key value.

cell.CommentLabel.text=[ResDiction objectForKey:@"Comment"];
cell.TimeLabel.text=[ResDiction objectForKey:@"CreateDate"];
cell.UserName.text=[ResDiction objectForKey:@"CreateUserName"];
cell.UserName.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.numberOfLines = 8;
cell.TimeLabel.adjustsFontSizeToFitWidth=YES;

How do I let the label's content determine the cell height?

Arun
  • 624
  • 4
  • 18
  • please answer@Mayank patel – Arun Dec 25 '15 at 09:33
  • there are several tutorial about this. it's all about setting the right constraints in your custom cell. http://www.raywenderlich.com/87975/dynamic-table-view-cell-height-ios-8-swift – Mat Dec 25 '15 at 09:34
  • did u add these: `tableView.estimatedRowHeight = 44.0` and `tableView.rowHeight = UITableViewAutomaticDimension` in your `viewDidLoad`? – aaisataev Dec 25 '15 at 09:36
  • Check this tut: [Dynamic cell height based on label](http://useyourloaf.com/blog/self-sizing-table-view-cells.html) – Tj3n Dec 25 '15 at 09:36
  • Have you added the size of the cell by yourself? If yes, remove it and let the system decide. – Rashwan L Dec 25 '15 at 09:36
  • okay i will try that out – Arun Dec 25 '15 at 09:39
  • Possible duplicate of [Using Auto Layout in UITableView for dynamic cell layouts & variable row heights](http://stackoverflow.com/questions/18746929/using-auto-layout-in-uitableview-for-dynamic-cell-layouts-variable-row-heights) –  Dec 25 '15 at 10:05

1 Answers1

0

You have to use

    -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableviwCell* cell=[tableview cellForRowAtIndexPath:indexPath];
cell.CommentLabel.text=[ResDiction objectForKey:@"Comment"];
cell.TimeLabel.text=[ResDiction objectForKey:@"CreateDate"];
cell.UserName.text=[ResDiction objectForKey:@"CreateUserName"];
cell.UserName.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.adjustsFontSizeToFitWidth=YES;
cell.CommentLabel.numberOfLines = 8;
cell.TimeLabel.adjustsFontSizeToFitWidth=YES;
float height= cell.cell.CommentLabel.frame.size.height+TimeLabel.frame.size.height+(all your UIFields you have used)

return height;
}

This method is will return the height for the cell while it is being created. The code inside will do calculate height of each uiField and combine them, this will work if you are arranging like stacks in UITableview cell, or you have to modify the code inside the function as per you arrangement.

Devang Tandel
  • 2,988
  • 1
  • 21
  • 43