UITableViewAutomaticDimension适合子视图

时间:2017-11-07 16:46:54

标签: ios swift autolayout uikit snapkit

我的应用程序有一个自动调整行高的表视图( UITableViewAutomaticDimension 用于heightForRow-Delegate-Function)。 我的自定义单元格具有子视图,可以具有不同的大小(它们包含始终不同数量的条形图)。

但是,自动单元格高度无法适应tableview单元格的子视图的这些不同大小。

如果我为tableview单元格中的子视图设置了一个约束(例如make.top.equalTo(self).offset(300) with snap kit),那么 只有有效 - 但由于这些子视图可以有不同的大小,这不是一个干净的解决方案。

你知道更好的方法吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

viewdidLoad上的

第一个:(委托中):

tableView.rowHeight = UITableViewAutomaticDimension
tableView.estimatedRowHeight = 140 //this estimation will improve performance (can be omitted), set the +/- size of most cases.

只是不要覆盖单元格高度委托。

<强>第二       在你的单元格中,你添加了包含字符的子视图。添加子视图约束,但更重要的是在该子视图和单元格之间添加垂直间距(这种情况是超级视图)。像图像一样。

enter image description here

如果子视图高度将设置单元格高度。只需确保你的字符在subView中设置subView高度(带约束)。

For more details, here there's a good step by step.