0

I can't get iOS 8 self sizing cells to work with the default cell style UITableViewCellStyleSubtitle and numberOfLines = 0. It seems that only default cell styles with multiple labels have this issue. The basic style works fine.

Here is sample project demoing the issue: https://github.com/stevemoser/SelfSizingDemo-DefaultCellStyle

Original code taken from this blog post that uses a custom table view cell: http://www.appcoda.com/self-sizing-cells/

Steve Moser
  • 7,647
  • 5
  • 55
  • 94
  • 1
    It seems that way. You'll have to use a custom `UITableViewCell` class with your own constraints, as the built-in styles don't appear to rely on exposed constraints. –  Jul 02 '15 at 15:56

1 Answers1

0

The basic cell types totally support self-sizing cells....You just need to remember to set the

self.tableView.estimatedRowHeight = 44;
self.tableView.rowHeight = UITableViewAutomaticDimension;

Add those 2 lines and like magic it will work for you

DBoyer
  • 3,062
  • 4
  • 22
  • 32
  • Hmm... I did. Do you have a working sample project that has multiline labels? When I modify any of the examples out there to use a default cell style and set numberOfLines = 0 they fail. – Steve Moser Jul 02 '15 at 19:45
  • Throw up the code you wrote because you shouldn't need more than those 2 lines to get self-sizing cells working with the basic cell types. Setting the estimatedRowHeight is required! – DBoyer Jul 02 '15 at 19:50
  • I didn't write any code. :) Just modified a the storyboard in a working project that had those two lines of code. Lets see if I can quickly post it. – Steve Moser Jul 02 '15 at 19:52
  • Make sure the heightForRowAtIndexPath delegate method is returning UITableViewAutomaticDimension if you are using that method – DBoyer Jul 02 '15 at 19:54
  • I'm not using that method. I edited my question with a sample project. – Steve Moser Jul 02 '15 at 19:57
  • Hmm well if you didn't end up doing the constraints properly for the name/address label it would screw up the sizing. Try removing the name/address labels and getting it to work with the built in textLabel then add in the other labels. – DBoyer Jul 02 '15 at 20:00
  • Ah, I think we have a misunderstanding. I'm not laying out the cell. I'm using a default cell style. – Steve Moser Jul 02 '15 at 20:02
  • What I mean is by adding more subviews even to a default cell style and adding constraints it will alter the cell's ability to self-size, one or more of those constraints are probably wrong – DBoyer Jul 02 '15 at 20:04
  • I've determined that it only happens on default cell styles with more than one label (UITableViewCellStyleSubtitle). When I try to add a label to default cell style I can't seem to add constraints between my labels and the default cell's label in IB. I imagine I could do it in code though. – Steve Moser Jul 02 '15 at 20:08
  • You can only do that in code yes, but almost all the time if your adding more subviews then ignore the cell's built in textLabel and just add your own and do the constraints yourself. – DBoyer Jul 02 '15 at 20:10
  • That feels like it defeats the purpose of using the default cell style in the first place. Why not use a custom cell at that point? – Steve Moser Jul 02 '15 at 20:12
  • Yeah almost every table view cell you make will require a custom subclass. The built in cell style is rarely used as-is. A settings screen is a good example where you may use it as-is, but thats about it. – DBoyer Jul 04 '15 at 04:32