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

六眼飞鱼酱① 提交于 2019-12-24 19:23:32

问题


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 :)


回答1:


  • If u have done everything correct and still u r 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].

  • Make sure you have set number of lines to 0, and all other automatic dimension stuff's.

  • IB Constraint Screenshot.

OutPut:




回答2:


You may consider using 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. https://developer.apple.com/documentation/uikit/uitableviewdelegate/1614998-tableview

This link may help: Dynamic tableViewCell height



来源:https://stackoverflow.com/questions/44753952/not-able-to-set-the-dynamic-height-of-table-view-cell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!