首先将您的 TableView rowHeight 赋予 UITableViewAutomaticDimension。它将允许 tableView 单元格的高度根据他的内容增长:
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return UITableViewAutomaticDimension
}
或
self.tableView.rowHeight = UITableViewAutomaticDimension
因为很容易先给出名称,所以您的 tableViewCell 中有:- ImageView、messageLabel、profileNameLabel 和 timeLabel。
第一步:
给你的 ImageView 约束 Top 和 Left 到 superView(Content View) 和 timeLabel 到 Right 到 superView 也对齐 将 Y 居中到您的 ImageView。
第二步:
现在在中间给你的 messageLable 约束 Top 到 superView,Left 到 imageView 和 Right 到 superView。
确保 messageLable 行号为 0。
第三步:
然后将您的 profileNameLable 约束 Top 和 Leading 设置为 messageLable。
第四步:
因此,现在要使您的 tableViewCell 高度将根据您的内容(messageLable 可变高度)增长。将您的 profileNameLabel bottom Grater than equal to(>=) 限制为 SuperView(ContentView) 例如 20
还有 ImageView 底部 Grater than equal to(>=) 对 SuperView(ContentView) 的约束,例如 20。所以当 messageLabel 有更高的高度它将推动单元格高度,当它具有正常高度时,单元格将从您的 ImageView 中获取高度。
Grater 然后等于 (>=) 约束(这基本上意味着始终保持 margin 例如 20 像素空间,因此它可以超过 20 像素但不能更少那么)