divya d
divya d

Reputation: 165

Multiline Label displaying extra empty spaces in table view cell in Xcode 9

I displayed multiline label in the table view using Autolayout. Everything is well but cell height is very much bigger than the label size.For example, if my label height is 100, cell height should be 100 because I am returning heightForRowAtIndexPath as label height but its height is returning as 150 etc.

Then I tried by removing setPreferredMaxLayoutWidth for that label, In that case, the last line of that label is not displaying(Truncating).

Upvotes: 0

Views: 704

Answers (2)

divya d
divya d

Reputation: 165

Finally fixed that problem by adding following code in cellForRowAtIndex

    cell.bounds = CGRectMake(0, 0, CGRectGetWidth(tableView.bounds), 99999);
    cell.contentView.bounds = cell.bounds;
    [cell layoutIfNeeded];
    [cell.myLabel setPreferredMaxLayoutWidth:CGRectGetWidth(cell.myLabel.frame)];

Click here for more detail.

Upvotes: 2

Aura Abhishek
Aura Abhishek

Reputation: 76

Use

 mytableView.rowHeight = UITableViewAutomaticDimension
  mytableView.estimatedRowHeight = 100.0

instead of returning height from heightForRowAtIndexPath. And set all side autolayout constraint if your label height is dynamic. Set label's Lines property to zero. If u want label and cell height same then set leading, trailing, top, bottom constraint to zero without margin.

Upvotes: 2

Related Questions