14

(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
  • 2,412
  • 3
  • 25
  • 31
  • I marked the question as a dupe since the answer exists, but it is super long and drawn out. Short answer: Add the two lines of code listed above to your view or tableview controller, and make sure you have the bottom content constrained to the bottom of the cell view and the top content to the top of the cell (in addition to all other constraints) – Sean Oct 11 '15 at 08:28

1 Answers1

20

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.

Mike Taverne
  • 9,156
  • 2
  • 42
  • 58
  • 4
    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. – Sean Oct 11 '15 at 08:27
  • 1
    I confirm it: you need "UITableViewAutomaticDimension", "estimatedRowHeight" AND setting the internal constrains for the height in the table cell .xib files – Claus Mar 08 '17 at 13:56
  • 1
    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. – C0D3 Jun 04 '18 at 19:43
  • Thanks Mike it worked – Ahsan Mar 04 '19 at 16:17
  • 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. – MrAn3 Jun 19 '20 at 05:44