Having an issue whereby a UILabel within a UITableViewCell that is automatically sized has an incorrect frame when the view is loaded, but upon table scroll the label size magically updates so the frame is correct.
Here is the constraints for the label within the cell, no ambiguity here so the table should pick up the size automatically no problem:
I'm configuring a bit of the UI in code too, this can be seen below:
[self.label setText:localFieldObject.value];
[self.label setDelegate:self];
[self.label setLineBreakMode:NSLineBreakByWordWrapping];
[self.label setNumberOfLines:0];
[self.label layoutIfNeeded];
Note that if I break on layoutIfNeeded
, despite having just set the number of lines, this is nil. self.label
does have a value however.
I've also utilised the view debugger to inspect frames both before and after scroll, it appears that the cell is sized correctly on both occasions but the frame of the label within the cell is what is changing, yet the constraints are not changing. I've tried various suggestions from similar questions but nothing has solved the problem as of yet.
If you have any further info you'd like just ask; thanks in advance.
You need to call [cell layoutIfNeeded];
before returning the cell to force the layout to happen.
In my experience, try to avoid putting layout code in awakeFromNib
as this can then be overwritten before the view is shown to the user. Generally you won't need to call layoutIfNeeded
before returning the cell, but in some cases where you do, at the end of cellForRow
would be the place to do it.