在我的项目中,我有一个包含 3 个部分的静态 tableView。第二部分中的单元格包含一个动态填充的标签,因此具有动态高度。单元格应将其高度调整为标签的高度。这是我尝试过的,但没有成功:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.section == 0 {
return 44
} else if indexPath.section == 1 {
return UITableViewAutomaticDimension
} else if indexPath.section == 2 {
return 80
} else {
return 50
}
}
除自动尺寸外,所有部分的高度都已正确设置。有帮助吗?
最佳答案
在 viewDidLoad()
中设置这一行
tableView.rowHeight = UITableViewAutomaticDimension
然后写这个table view
方法
func tableView(tableView: UITableView, estimatedHeightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat
{
return UITableViewAutomaticDimension
}
还要确保您已正确使用自动布局
。
并且您已经设置了 label = 0 的行数
关于ios - tableView 没有设置自动行高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38356782/