我有很多 customCells,每个单元格可能有一个按钮,其作用类似于文本框或文本区域。当 View 加载时,它们的高度非常小。
override func viewDidLoad() {
super.viewDidLoad();
self.view.backgroundColor = UIColor.groupTableViewBackground
self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
self.tableView.estimatedRowHeight = 100
tableView.rowHeight = UITableViewAutomaticDimension
self.tableView.setNeedsLayout()
self.tableView.layoutIfNeeded()
}
自动布局是否适用于原型(prototype)单元格? 如果不是如何实现呢? 我尝试使用 HeightforrowAtIndexPath 方法布局只有在滚动后才看起来不错。 View 的初始加载不会带有良好的布局。
下面是我的代码。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (eleData.elementType=="apple")
{
return 90;
}
else if (eleData.elementType=="banana")
{
return 50;
}
else
{
return 30;
}
}
任何建议。 提前谢谢你。
最佳答案
快速谷歌搜索给你你的答案......
UITableView dynamic cell heights only correct after some scrolling
如果需要,您需要在 cellForRowAtIndexPath 方法中返回单元格之前添加布局:
cell.layoutIfNeeded()
关于ios - 无法快速设置 CustomCells 的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41493432/