Samarth Kejriwal
Samarth Kejriwal

Reputation: 1176

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: Constraints for my content view of the table view cell

Please have a look at the constraints as well. Thanks in advance :)

Upvotes: 0

Views: 108

Answers (2)

Gokul G
Gokul G

Reputation: 2096

  • 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. enter image description here

  • 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.

enter image description here

OutPut:

enter image description here

Upvotes: 1

Mihir Thanekar
Mihir Thanekar

Reputation: 518

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

Upvotes: 0

Related Questions