简体   繁体   English

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

[英]Not able to set the dynamic height of table view cell?

I am having the problem in setting the dynamic height for my table view cell.I have used the following code to set it, but it's not working in fact now the cell height has even increased. 我在为表格视图单元格设置动态高度时遇到问题,我使用以下代码对其进行设置,但实际上现在单元格的高度甚至增加了,但它不起作用。

See this the code I have added in my project: 参见我在项目中添加的代码:

 override func viewDidLoad() {
super.viewDidLoad()

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

}

This the screenshot for the constraints of the table view cell contents: 此屏幕快照显示了表视图单元格内容的约束: 表格视图单元的内容视图的约束

Please have a look at the constraints as well. 请同时查看约束。 Thanks in advance :) 提前致谢 :)

  • If u have done everything correct and still ur unable the get desired output means the issue will be with constraints. 如果您所做的一切正确无误,但仍然无法获得所需的输出,则表示问题将出在约束之下。

  • I have made a simple constraint layout, use this skeleton as reference and re-constraint your cell. 我做了一个简单的约束布局,使用此骨架作为参考并重新约束您的单元格。 在此处输入图片说明

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

  • Make sure you have set number of lines to 0, and all other automatic dimension stuff's. 确保将行数设置为0,并设置所有其他自动标注内容。

  • IB Constraint Screenshot. IB约束截图。

在此处输入图片说明

OutPut: 输出:

在此处输入图片说明

You may consider using func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat . 您可以考虑使用func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat It's a method in the UITableViewDelegate and you can use it to return a specific height for the rows you want. 它是UITableViewDelegate中的一种方法,您可以使用它为所需的行返回特定的高度。 https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview

This link may help: Dynamic tableViewCell height 该链接可能有所帮助: 动态tableViewCell高度

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM