Search code examples
xcodeuitableviewcell

Unable to get dynamic table cell size working in XCode 6 iOS 8


I'm trying to adopt the adaptive layout in a new app and have learned quite a bit. For the last portion I had to dump text into a table and have the cells adjust to stay adaptive, seemed easy enough.

I read through a few tutorials and for iOS 8 it looks like it just comes down to two things:

  1. Having estimatedRowHeight and rowHeight = UITableViewAutomaticDimension set
  2. Having constraints set up right to create pressure on the cell itself

Shamefully my entire day has gone out the window trying to master those mere two points... I downloaded code provided from Keith Harrison on self sizing table view cells to see it work. I duplicated his cell into my larger project and it worked, however none of my own cells worked. I deconstructed everything in his project and minimized it to the point where little is left then tried making a similar cell from scratch. I've gone through and verified every constraint, every hugging/resistance setting, etc, and yet mine fail to expand the cell height.

If anyone can point out what I'm doing wrong (and it must be tiny because everything I see is the same) I have uploaded a minimized version of his example here (GitHub).

With this example I tried a cell with a single multiline label with constraints all around along with a cell as close as possible to his cell. I found that in many of my attempts everything looked identical however I would get a UIView-Encapsulated-Layout-Height constraint issue. With my last attempt I reduced the priority of the vertical spacing to the content container to 999 from 1000.

Things I have checked or tried with no effect:

  • Contraints are to superview (content container, not cell)
  • Label is set to 0 lines
  • Hugging and Resistance priorities the same as working cells
  • Reloading table once loaded

Solution

  • I have my solution working now after another long stab at it. Two things:

    1. The UITableViewCells, while identical in every way in the storyboard had a rect tag within the storyboard source with a defined width and height. I looked everywhere for the values in IB with no luck. Taking that out fixed my problem for the GitHub test project I posted.
    2. After moving over to the project I was initially working on the cells still were not resizing, but would when orientation was changed. I had to add a cellForRowAtIndexPath method to call setNeedsDisplay and layoutIfNeeded on the cell being requested.

    Following the above everything appears to be working.