我使用静态 TableView 来显示一些数据,因为我的数据是动态的,所以我需要一个可以根据内容调整大小的单元格。我已经覆盖了 heightForRowAtIndexPath:
override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
if indexPath.row == 4 {
let size = itemDescription.systemLayoutSizeFittingSize(UILayoutFittingCompressedSize)
return size.height
}
return super.tableView(tableView, heightForRowAtIndexPath: indexPath)
}
我已经尝试了 UILayoutFittingCompressedSize
和 UILayoutExpandedSize
,两者都会返回一个 CGSize
,高度为 140,不足以显示我的文本。
我还尝试增加 UILabel
中的行数,但它也不起作用。
在 Storyboard 上,我的 Cell 是一个基本的 TableViewCell
。
有人知道发生了什么事吗?
最佳答案
要使用自动调整单元格,您不需要覆盖 heightForRowAtIndexPath
。覆盖 estimatedHeightForRowAtIndexPath
并返回估计值或 UITableViewAutomaticDimension
。而且,重要的是,您必须对单元格的所有 4 个边都有明确的约束。对于 UILabel
,指定 0 作为行数以启用文本自动高度。
关于ios - systemLayoutSizeFittingSize 没有为我的 UILabel 返回足够大的尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31974959/