【发布时间】:2021-06-25 02:53:27
【问题描述】:
我正在使用具有动态单元格高度的 UITableView 构建 cmets 功能。我正在使用制图框架以编程方式设置每个单元格内容的约束,因为此表视图未在情节提要中设置。
我的评论标签与下面的单元格重叠有问题。 带有简短注释字符串的单元格看起来不错,这是一个示例 SS:
我设置了tableView.estimatedRowHeight = 60,单元格是clipsToBounds = false
和
func tableView(_: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
UITableView.automaticDimension
}
以下是我使用制图框架为 Cell 的不同子视图设置的约束:
// User Image View
constrain(self, userImageView) {
$1.height == 36.0
$1.width == 36.0
$1.left == $0.left + 16.0
$1.top == $0.top + 12.0
$1.bottom == $0.bottom - 12.0 ~ UILayoutPriority(500)
}
// Comment Label
constrain(self, commentLabel, userImageView) {
$1.top == $2.top - 6.0
$1.right == $0.right - (18.0 + Geometry.likeButtonWidth)
$1.left == $2.right + Geometry.messageLabelLeftOffset
}
// Bottom view - ( comment_time / LikeCount / Reply )
constrain(self.contentView, messageLabel, bottomView, userImageView) { contentView, msgLabel, bottomView, userImageView in
bottomView.top == msgLabel.bottom
bottomView.right == msgLabel.rightMargin
bottomView.left == userImageView.right + Geometry.messageLabelLeftOffset
// contentView.bottom == bottomView.bottom // very tall cell is overlapping cells below with this line
// contentView.height == msgLabel.height + 20 // cell is twice as tall, leaving large empty gap, but not overlapping
}
评论标签没有设置底部约束。
bottonView 的顶部设置为评论标签的底部,也没有底部约束。我认为这允许动态高度工作。 在底部视图的约束中没有使用上面注释掉的约束,它仍然是重叠的。我知道重叠是由单元格上的 clipsToBounds 设置为 false 引起的,因此单元格的高度是问题所在。
看起来是这样的:
如何让单元格高度适应内容?
【问题讨论】:
-
我不知道制图是什么,但这意味着您在视图中缺少垂直约束。如果你不垂直约束你所有的东西,表格视图就不能计算单元格的高度。
-
@Desdenova Cartography 是一个以编程方式设置约束的框架,如上图所示,即“constrain(...)”,非常有用。好的,我会再看看所有的约束,看看我是否遗漏了什么。
标签: ios swift uitableview constraints cartography