iOS Auto-Layout: Dynamic height for table view cell

Chrizzor picture Chrizzor · May 21, 2013 · Viewed 9.1k times · Source

I have a table view with a bunch of cells (custom cell, which only has its content view).

In tableView:cellForRowAtIndexPath: I'm adding a predefined UIView (which has several subviews) to the content view of the custom cell. I set up all constraints for the UIView and its subviews before.

Last but not least, I set the vertical and horizontal constraints for the content view of my custom cell (superview) and the UIView, which was added before (subview).

The constraint strings look like this:

    H:|[view]|
    V:|[view]|

Unfortunately, I still get the default height for all table view cells. I'm wondering If there's a way to let auto layout do the calculation of the height automatically according to content size.

Answer

smileyborg picture smileyborg · Oct 10, 2013

Check out my detailed answer to this question here: https://stackoverflow.com/a/18746930/796419

It takes a bit of work to set up, but you can absolutely have Auto Layout constraints driving a completely dynamic table view without a single hardcoded height (and let the constraint solver do the heavy lifting and provide you with the row height).