UITableView Automatic Dimension Not Working Correctly

7.7k Views Asked by At

I have a tableview that is getting populated with data from Firebase. However, when resizing the tableview using automatic dimension, some text is shown getting cut off.

Here is my Storyboard with constraints set to top, bottom, right, and left.

enter image description here

It is working fine when there is not alot of text as shown here.

enter image description here

However, when I fill the cell with alot of text this occurs.

enter image description here

Here is the code that I am currently using.

func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if (tableView == questInformationTableView) {
            return 50
        }
        else if (tableView == questWalkthroughTableView) {

            return UITableView.automaticDimension
        }

        return 0
    }
    override func viewDidLoad() {
          super.viewDidLoad()
          questWalkthroughTableView.estimatedRowHeight = 250
          questWalkthroughTableView.rowHeight = UITableView.automaticDimension
        }

    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
         return UITableView.automaticDimension
        }

I have also set numberOfLines to 0 and set the label to .sizeToFit()

4

There are 4 best solutions below

2
On

Don't make the bottom constraint greater than or equal to just set it like the top constraint. Take away the estimatedHeightForRowAt and heightForRowAt functions. Your view did load declarations are sufficient. When you reload data also call self.view.layoutIfNeeded

0
On

I usually set the automatic height dimension and then do a reload in the viewDidLoad like this

override func viewDidLoad() {
    super.viewDidLoad()

    tableView.rowHeight = UITableViewAutomaticDimension
    tableView.estimatedRowHeight = 44

    tableView.reloadData()   
}
0
On

Suggest to check

  1. automatic row height is enabled inside the table cell enter image description here

  2. check if the label is expandable and its bottom constraint is set to superview. Adding screenshot showing list of constraints of a sample cell I am using with dynamic height.

enter image description here

  1. Even if this doesn't fix your issue, suggest to reset all constraints and set it again for your cell.
0
On

For a similar issue, I removed the tableView.estimatedRowHeight assignment and kept tableView.rowHeight = UITableView.automaticDimension and everything started working. Only seemed to be an issue on iPad though.