UITableViewCell cannot get correct width with systemLayoutSizeFittingSize

I am using autolayout + xib to plot the cell and the cell height is dynamic so I use systemLayoutSizeFittingSize

to get the height for

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath;

      

my code looks like this:

enter image description here and this will print the logs like this: enter image description here

so that we can find that the size return from systemLayoutSizeFittingSize

might be wider than my device's screen (iPhone5: 320 dots), how could this happen? (and I tried adding layoutIfNeeded

and updateConstraintsIfNeeded

, but the problem still exists.

And I was using Xcode 6, the xib file size is not fixed.

PS: my view contains a multiline label, I am printing the preferredMaxLayoutWidth of that label and I found the preferredMaxLayoutWidth is very big too, maybe this is the problem. But how do you fix it?

+3


source to share


1 answer


Yes, it is a problem of wrong preferredMaxLayoutWidth, fix by adding this line to custom cell updateConstraints self.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth (self.contentLabel.frame);

PS



in fact, I found that the subView element cannot get the correct width after the layoutSubview cell is called, so if you want to make the preferredMaxLayoutWidth based on the label width, it is better to set the preferredMaxLayoutWidth to layoutIfNeeded but not layoutSubViews

+2


source







All Articles