无法设置表格视图单元格的动态高度?

时间:2017-06-26 06:14:08

标签: ios uitableview constraints

我在设置表格视图单元格的动态高度方面遇到了问题。我使用了以下代码来设置它,但实际上它不起作用,现在单元格高度甚至已经增加。

请参阅我在项目中添加的代码:

 override func viewDidLoad() {
super.viewDidLoad()

        tableview.rowHeight = UITableViewAutomaticDimension
        print(tableview.rowHeight)
        tableview.estimatedRowHeight = 120

}

这是表视图单元格内容约束的屏幕截图: Constraints for my content view of the table view cell

请查看约束。在此先感谢:)

2 个答案:

答案 0 :(得分:1)

  • 如果你做的一切都是正确的,但仍然无法获得所需的输出意味着问题将受到限制。

  • 我做了一个简单的约束布局,使用这个骨架作为参考并重新约束你的单元格。 enter image description here

  • 将(W:20,H:20)添加到[img]。

  • 确保您将行数设置为0,以及所有其他自动维度内容。

  • IB Constraint Screenshot。

enter image description here

输出:

enter image description here

答案 1 :(得分:0)

您可以考虑使用func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat。它是UITableViewDelegate中的一个方法,您可以使用它返回所需行的特定高度。 https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview

此链接可能会有所帮助: Dynamic tableViewCell height

相关问题