无法在 swift 中设置 CustomCells 的高度
unable to Set Height of the CustomCells in swift
我有很多自定义单元格,每个单元格可能有一个按钮,其作用类似于文本框或 textArea.When 视图加载它们的高度非常小。
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()
}
我在单元格上定义了以下约束
自动布局是否适用于原型单元格?
如果不是如何实现呢?
我尝试使用 HeightforrowAtIndexPath 方法布局只有在滚动后才看起来不错。视图的初始加载不会带有良好的布局。
下面是我的代码。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (eleData.elementType=="apple")
{
return 90;
}
else if (eleData.elementType=="banana")
{
return 50;
}
else
{
return 30;
}
}
任何建议。
提前谢谢你。
快速 google 搜索给你答案...
UITableView dynamic cell heights only correct after some scrolling
如果需要,您需要在 cellForRowAtIndexPath 方法中返回单元格之前添加布局:
cell.layoutIfNeeded()
我有很多自定义单元格,每个单元格可能有一个按钮,其作用类似于文本框或 textArea.When 视图加载它们的高度非常小。
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()
}
我在单元格上定义了以下约束
自动布局是否适用于原型单元格? 如果不是如何实现呢? 我尝试使用 HeightforrowAtIndexPath 方法布局只有在滚动后才看起来不错。视图的初始加载不会带有良好的布局。
下面是我的代码。
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
if (eleData.elementType=="apple")
{
return 90;
}
else if (eleData.elementType=="banana")
{
return 50;
}
else
{
return 30;
}
}
任何建议。 提前谢谢你。
快速 google 搜索给你答案...
UITableView dynamic cell heights only correct after some scrolling
如果需要,您需要在 cellForRowAtIndexPath 方法中返回单元格之前添加布局:
cell.layoutIfNeeded()