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

user3667192
  • 103
  • 1
  • 6
  • *"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? – DonMag Nov 15 '18 at 19:29
  • that image is one cell. when I enable scrolling i can see the full text. – user3667192 Nov 15 '18 at 20:21
  • Sorry, not clear. Can you change the image to show how the full table looks with multiple rows? – DonMag Nov 15 '18 at 21:22
  • updated to include full View Controller – user3667192 Nov 16 '18 at 14:32
  • Can you share a simulator screenshot in runtime please? – DionizB Nov 16 '18 at 14:43
  • @user3667192 - sorry, I meant show the full table / view while the app is running. – DonMag Nov 16 '18 at 14:45
  • Updated with the Tableview During Runtime. Thanks. – user3667192 Nov 16 '18 at 16:35
  • @user3667192 - ok, the problem is that you are setting the height of your tableView... `.automaticDimension` is only related to cell / row height, not tableView height. – DonMag Nov 16 '18 at 17:49
  • how would i set the cell to .automaticDimension and not the tableview? – user3667192 Nov 17 '18 at 17:19
  • @user3667192 - 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? – DonMag Nov 19 '18 at 15:04
  • i have a tableview with a section header and one cell. – user3667192 Nov 19 '18 at 20:18
  • @user3667192 - 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). – DonMag Nov 20 '18 at 12:52

4 Answers4

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

Alexander
  • 1,424
  • 18
  • 23
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()   
}
Maria
  • 4,471
  • 1
  • 25
  • 26
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.
Sumit
  • 874
  • 9
  • 10
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.

landnbloc
  • 498
  • 4
  • 10