Search code examples
iosswiftxcodeuitableviewxcode-storyboard

Q: How can I change the cell size in UITableView, to show all the labels?


I nearly finished making my tableview cell and all the contents in data model shows up in the UI so I think the connection is fine. The problem is that my prototype cell shows only part of what it's supposed to show. I have 3 labels in the cell, but it only shows one and a half. the text is stuck to the left(I wanted to put it in center), and the textlabel is cut off so it doesn't show in the UI. It's supposed to look like this.

I tried

tableView.estimatedRowHeight = tableView.rowHeight tableView.rowHeight = UITableView.automaticDimension

this code in the ViewController to make my cell to automatically set height to my labels, but it didn't work. I also used vertical stack view and made constraints so it fills the cell fully, and it doesn't seem to make a change.

Please help!


Solution

  • You have to use implement the UITableViewDelegate protocol and the function

    func tableView(_ tableView: UITableView, 
         heightForRowAt indexPath: IndexPath) -> CGFloat
    

    where you can return the custom height for each cell