我有UITableViewCell,其中多个组件充当行,由UIVabel中的UILabel,UIView和UIStackView的混合元素组成。
单元格内的每个组件都有高度约束。我希望通过在运行时将高度约束的常量设置为零或21来确定单元格的高度是动态的,取决于是否有要显示的内容。
// more codes like below
if let mealPref = passenger.mealPref {
mealPrefHeight.constant = 21
let title = R.string.localizable.meal_request.localized()
mealPrefLabel.text = "• \(title): \(mealPref)"
} else {
mealPrefHeight.constant = 0
}
// more codes like above
它不起作用。
我有类似的problem back then,但我认为情况不同。我读过this,this和this。
答案 0 :(得分:1)
您是否添加了viewDidLoad
tableView.estimatedRowHeight = 44 // for assumption
tableView.rowHeight = UITableViewAutomaticDimension
答案 1 :(得分:0)
尝试移除高度约束。
if let mealPref = passenger.mealPref {
let title = R.string.localizable.meal_request.localized()
mealPrefLabel.text = "• \(title): \(mealPref)"
}
答案 2 :(得分:0)
您只需在UITableViewCell's
方法中更改UITableViewDelegate
高度:
可选的func tableView(_ tableView:UITableView,heightForRowAt indexPath:IndexPath) - > CGFloat的
示例:强>
override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
{
if indexPath.row == 0
{
return 126.0
}
else
{
return 90.0
}
}
您可以根据需要更改高度。在这里,我只使用行号来改变高度。
<强>截图:强>