UITableView Automatic Dimension Not Working Correctly
Asked Answered
D

4

8

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

Dispensable answered 15/11, 2018 at 17:34 Comment(12)
"when resizing the tableview using automatic dimension, some text is shown getting cut off" ... do you mean the image you've shown is the entire tableview? Or just one cell / row?Luster
that image is one cell. when I enable scrolling i can see the full text.Dispensable
Sorry, not clear. Can you change the image to show how the full table looks with multiple rows?Luster
updated to include full View ControllerDispensable
Can you share a simulator screenshot in runtime please?Pontianak
@Dispensable - sorry, I meant show the full table / view while the app is running.Luster
Updated with the Tableview During Runtime. Thanks.Dispensable
@Dispensable - ok, the problem is that you are setting the height of your tableView... .automaticDimension is only related to cell / row height, not tableView height.Luster
how would i set the cell to .automaticDimension and not the tableview?Dispensable
@Dispensable - are you only displaying one cell in that tableView? If so, tableView may not be the best element to use... If you're displaying multiple cells, why do you have the tableView constrained to only that height?Luster
i have a tableview with a section header and one cell.Dispensable
@Dispensable - ok... so why do you have the tableView set so short? Constrain the bottom of the tableView to the bottom of the view (with 20-pt padding, or whatever you're using for the sides).Luster
M
1

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

Masonite answered 15/11, 2018 at 17:48 Comment(2)
I have made the changes but nothing changed.Dispensable
Remove the bottom constraint did the job for me :)Retarder
A
1

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()   
}
Amatol answered 16/11, 2018 at 16:42 Comment(0)
S
1

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.
Sabbatical answered 14/4, 2020 at 16:13 Comment(0)
B
0

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.

Bezel answered 17/1, 2022 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.