0

I'm having a lot of issues getting my custom cells to use the dynamic row height.

Here is the xib of my custom cell.

XIB

In viewDidLoad for my table view, I set these values:

self.tableView.estimatedRowHeight = 80.0
self.tableView.rowHeight = UITableViewAutomaticDimension

but it doesn't seem to use the dynamic size when it loads the table.

Below is my cellForRowAtIndexPath function.

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    // Configure the cell...

    let cell = Bundle.main.loadNibNamed("RedditCell", owner: self, options: nil)?.first as? RedditPostCellTableViewCell

    let post = self.model?.getPostAt(index: indexPath.row)
    cell?.title.text = post?.title
    cell?.author.text = post?.author
    cell?.upvotes.text = "\(post?.upvotes ?? -1)"

    return cell!
}

Table view when I run my app

I can't figure out why this isn't working. Any help would be appreciated!

Megan Wood
  • 3
  • 1
  • 5

3 Answers3

1

Please try to set the height in the heightForRowAt

override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    self.tableView.estimatedRowHeight = 80
    return UITableViewAutomaticDimension
}
Vini App
  • 7,339
  • 2
  • 26
  • 43
0

You don't need to pre-calculate the height. The problem is with your constraints in your custom UITableViewCell. Your constraints should be set up the way to force UITableViewCell to expand to show the whole label or any other content. Just setup constraints similar to my short example project: https://www.transfernow.net/216sw6l1bler?lng=en, and cell height will be automatically set, as you expected.

Screenshot: enter image description here

Vladimir88dev
  • 733
  • 1
  • 10
  • 19
  • Just tested your project. What happens if you remove your "labellabellabellabellab.." Placeholder from UIStoryboard? –  Sep 04 '17 at 23:13
  • It works well. I made this example very quickly and I made a small error in layout, but it works in every case. Here is the example without "label label label..." and without errors in layout - https://www.transfernow.net/16jie3d161dr?lng=en – Vladimir88dev Sep 04 '17 at 23:16
  • Also, as @Martin Muldoon said, user has also to set number of lines = 0 and word wrap, but I didn't mention it because it is already set as I can suppose from the screenshot – Vladimir88dev Sep 04 '17 at 23:19
  • Yeah I saw, last time I tested this some years ago this was a mess, not sure why it works so perfect now, even when the documentation clearly states that we need fixed heights, either I missed something or it was some bug in older iOS versions. Seems to work well now, upvoted you. –  Sep 04 '17 at 23:20
  • Thank you! this is really helpful. Although I'm having a hard time understanding the difference between your constraints and mine. Can you point out what specifically I should do differently? – Megan Wood Sep 04 '17 at 23:27
  • @MeganWood Check out my updated answer. I found the problem. –  Sep 04 '17 at 23:28
  • @MeganWood I think that most important part of the layout which you don't have is the constraint between label bottom and cell bottom set it to greater than or equal to some value (padding). It will force the cell to enlarge its height. – Vladimir88dev Sep 04 '17 at 23:30
0

In addition to setting the constraints so that the height of all of the elements in the cell + padding can be used to calculated the overall cell height, you must set the number of lines of your labels to zeros and set them to word wrap if these must grow based on content as well.

Martin Muldoon
  • 3,388
  • 4
  • 24
  • 55