gpt4 book ai didi

ios - UITableViewCell 无法使用 systemLayoutSizeFittingSize 获得正确的宽度

转载 作者:行者123 更新时间:2023-12-02 03:32:15 25 4
gpt4 key购买 nike

我使用autolayout+xib构建单元格,并且单元格的高度是动态的,所以我使用systemLayoutSizeFittingSize来获取单元格的高度

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

我的代码是这样的:

enter image description here这将打印如下日志: enter image description here

所以我们可以发现,systemLayoutSizeFittingSize返回的尺寸可以比我的设备屏幕(iPhone5:320点)更宽,这是怎么发生的? (我尝试添加 layoutIfNeededupdateConstraintsIfNeeded,但问题仍然存在。

我使用的是 Xcode 6,其中 xib 文件大小不固定。

PS:我的 View 包含一个多行标签,我打印了这个标签的preferredMaxLayoutWidth,我发现preferredMaxLayoutWidth也很大,可能这就是问题所在。但是如何解决它?

最佳答案

是的,这是preferredMaxLayoutWidth错误的问题,通过在自定义单元格的updateConstraints中添加这一行来修复 self.contentLabel.preferredMaxLayoutWidth = CGRectGetWidth(self.contentLabel.frame);

附注

事实上,我发现单元格的layoutSubview调用后,单元格的 subview 可能无法获得正确的宽度,所以如果你想根据标签的宽度设置preferredMaxLayoutWidth,你最好在layoutIfNeeded中设置preferredMaxLayoutWidth,而不是在布局 subview

关于ios - UITableViewCell 无法使用 systemLayoutSizeFittingSize 获得正确的宽度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27222171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com