UITableViewCell 自调整大小搞砸了

UITableViewCell self sizing mess up

提问人:Alex171 提问时间:11/11/2023 更新时间:11/11/2023 访问量:37

问:

我正在尝试使我的UITableViewCell像这样:enter image description here

我决定添加一个 UIView 来包含我的文本,以便我可以制作边框。这是我的.xib配置enter image description here

我的 ViewController 的 TableView 设置

        tableView.delegate = self
        tableView.dataSource = self
        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.estimatedRowHeight = 100
        if #available(iOS 15.0, *) {
            tableView.sectionHeaderTopPadding = 0.0
        }
        self.tableView.isScrollEnabled = false

但事情的结局是这样的

enter image description here

这与我的约束设置有关吗?或者我遗漏了什么,有人可以指出来吗? 提前致谢

Swift UITableView

评论


答:

0赞 son 11/11/2023 #1

您缺少这两个标签之一与边框视图之间的约束,这将使单元格的高度动态化。这两个标签也是 Y 的中心,因此您可以使它们中的任何一个具有底部约束,即,我将这样做并将约束底部值更改为 10。BorderViewAmountTypeLabel

enter image description here

这是输出:

enter image description here

只需简单地考虑所有子视图从上到下的约束,就会使单元格具有动态高度:

ContentViewTop
      |
 BorderViewTop
      |
    Label
      |
 BorderViewBottom
      |
ContentViewBottom

评论

0赞 Alex171 11/12/2023
它有效,谢谢@son
0赞 son 11/12/2023
不客气。