Dynamic Cell Size in Table View from a custom Xib

10,416

Make sure you have set constraints within your tableview cell that will force it to expand to the height you want.

In particular, make sure you have vertical constraints between the top of the cell and "Hello", between "Hello" and "Language", and between "Language" and the bottom of the cell.

If this doesn't work, you might try doing this in a prototype cell within one of your table view controllers, just to see if perhaps there is an issue using xibs for dynamic cells.

Share:
10,416

Related videos on Youtube

Sean
Author by

Sean

Product/Eng/Design - Hacker/Entrepenuer

Updated on June 30, 2022

Comments

  • Sean
    Sean almost 2 years

    (Warning, I'm an iOS newbie.) I have created a custom Table View Cell and have registered it, and implemented it, on two seperate Table View Controllers. When I run the app the custom xib content is truncated by the parent Table Cell View height.

    I'd like for each Table View to dynamically determine its height based on the height of the content in the custom Table View Cell xib.

    I have tried adding

        myTableView.rowHeight = UITableViewAutomaticDimension
        myTableView.estimatedRowHeight = 160.0
    

    to one of the Table View Controller's viewDidLoad function, but no luck.

    Custome Table View Cell

    Separate Table View Controllers implementing custom xib

    Height of Table View 1

  • Sean
    Sean over 8 years
    Thanks Mike, that worked. Must have missed that all too important final constraint on the bottom. The "UITableViewAutomaticDimension", and "estimatedRowHeight" needed to exist as well.
  • Claus
    Claus over 7 years
    I confirm it: you need "UITableViewAutomaticDimension", "estimatedRowHeight" AND setting the internal constrains for the height in the table cell .xib files
  • C0D3
    C0D3 about 6 years
    I also wanna point out that the constraints need to be hooked up to the top and bottom contentView and NOT to the safe area.
  • Ahsan
    Ahsan over 5 years
    Thanks Mike it worked
  • MrAn3
    MrAn3 about 4 years
    I was having a problem because I had: textView.translatesAutoresizingMaskIntoConstraints = true in a UITextView inside my custom cell class. Removing it (along with your suggestions) solved it.