0
votes

I have a TableViewController with a custom UITableViewCell containing a single UILabel. The label will receive variable lengths of text, and should resize in height accordingly. I want to use auto layout, iOS10++.

However, it seems that the cell is preventing the label from expanding its height. I have constraints on the label to pin top, bottom, left and right to the cell's contentView. The label number of lines = 0, and is set to line break mode = WordWrap. I have set the self.tableview.rowHeight to UITableViewAutomaticDimension, and have set the estimated row height to various sizes with no success. I have increased (and decreased) the label's content hugging priority and and the vertical compression resistance, but this has no effect.

This sounds like a duplicate of so many other questions, but none I have read has solved my problem.

Some clues I have noticed: 1) If I remove the label's bottom constraint, the label expands correctly, but (of course) the cell doesn't expand, so the label cannot be fully seen after it expands below the bottom of the cell. So I conclude that the cell is preventing the label from expanding. 2) if I rotate the tableview to landscape and back to portrait, the first cell expands correctly. So something that occurs during the rotation solves the problem at least for the first cell, and also proves that the cell and label can expand as required.

I feel something is not right, but cannot figure it out. I am very close to going back to the old version of calculating the height manually and returning it in heightForRowAtIndexPath delegate method.

I would appreciate any suggestions. Thanks in advance.

1

1 Answers

0
votes

I finally figured it all out.

In summary, I was configuring the cell (including setting the label's text) in tableView willDisplayCellAtIndexPath...

But it seems (obvious really) that for the autoresizing to work, the cell must be configured in tableView cellForRowAtIndexPath. Moving the configuration into cellForRowAtIndexPath and suddenly everything started working perfectly.

Hope this helps anybody who has the same problem. I struggled with it for days.