untitled
untitled

Reputation: 405

Views not moving when UILabel's height grows

I have a UITableView with custom cells (with automatic heights). In these cells, I have two UILabels, a title and a body. I've set the title to have a maximum of 3 lines, and the body to have unlimited lines.

When the cell is loaded (through tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell), I set the title to "Waiting for Data" and the body to "Hello world". At this point, I send off a network request for some information, and when the request comes back, I then update the title UILabel.

However, it seems that when the new text causes UILabel's height to change (e.g. the text needs 2 or 3 lines to display), weird things occur. Strangely, either the title grows and compresses the body label, or the title doesn't grow and instead remains one line, ellipsized/truncated.

If I scroll away and scroll back, causing the cell to be recreated, the issue does not occur. I'm assuming this is because I cache the network response, and when creating the cell, if the response has been cached I directly set the title to the response. Based on this, I'm thinking that perhaps autolayout is not being called again when I modify the title label.

Initial cell:

enter image description here

After network response:

Case where title does not wrap into lines:

enter image description here

Case where title wraps into lines, but compresses the details label

enter image description here

In both the resulting cells, the title has also extended over/compressed the time label.

Should I be calling some method to re-layout my cell when this occurs? Are my constraints perhaps incorrect?

Upvotes: 0

Views: 61

Answers (2)

psk
psk

Reputation: 192

If you used vfl to manage vertical constraint for labels like this:

 V:|-[myLable]-|

Change it to this:

 V:|[mylabel]|

Upvotes: 0

Kishore Kumar
Kishore Kumar

Reputation: 4375

set this to your UILabel:

 yourUILabel.numberOfLines=0;
 yourUILabel.adjustsFontSizeToFitWidth=YES;

Try now :)

Upvotes: 1

Related Questions