我有一个 UITableView,其中填充了自动调整大小的单元格。UITableView
设置相当简单:
tableView.estimatedRowHeight = 70
tableView.rowHeight = UITableViewAutomaticDimension
就像苹果在这里推荐的那样:https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html https://developer.apple.com/library/content/documentation/UserExperience/Conceptual/AutolayoutPG/WorkingwithSelf-SizingTableViewCells.html
要启用自动调整表格视图单元格大小,您必须设置表格视图的
UITableViewAutomaticDimension 的 rowHeight 属性。你还必须
为estimatedRowHeight 属性分配一个值。一旦两者
设置这些属性后,系统使用自动布局来计算
行的实际高度。
配置单元格时,我还禁用/启用一些约束以实现所需的外观。这就是事情变得有趣的地方。在重新使用单元之前,单元布局不会更新。字面上地。您可以致电layoutIfNeeded()
, setNeedsLayout()
, layoutSubviews()
或任何其他方法,您都无法强制单元格更新其布局。
所有其他方面都工作得很好:标签确实会更改其文本,您可以隐藏/取消隐藏视图,但布局会卡住,直到重新使用单元格为止。
问题:是什么原因导致这种行为以及如何避免这种行为?
我也遇到了你的问题。而不是删除
tableView.estimatedRowHeight = 70
我刚刚在 cellForRow 方法的末尾添加了一个layoutIfNeeded,就在返回单元格本身之前:
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "identifier", for: indexPath) as? MyCustomCell
...
cell?.layoutIfNeeded()
return cell!
}
结果:单元布局始终完美,无论是第一次还是每次重复使用后。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)