2

I have a table view controller with a prototype cell with style "Left Detail."

-In my viewDidLoad() I have self.tableView.estimatedRowHeight = 40 and self.tableView.rowHeight = UITableViewAutomaticDimension like many solutions have said to do. However, when my detail label gets too big (like with the description cell), the cell doesn't grow with the text.

-I tried to reload the data in viewDidAppear(animated:) as well, but that didn't help.

-I don't have anything in my heightForRowAtIndex func right now, but have tried to set that too and it didn't fix my problem.

-I also tried cell.sizeToFit() in my cellForRowAtIndexPath after the cells are populated.

Am I missing something obvious? Or is there something in the "Left Detail" style that is restricting the cell height?

Tessa
  • 176
  • 13
  • Are you using auto layout? Cells aren't dynamic with `estimatedRowHeight` and `UITableViewAutomaticDimension` without auto layout. – tktsubota Feb 16 '16 at 22:03

1 Answers1

0

You're not missing anything obvious that I'm aware of, but likely running into an edge case involving the built-in cell style. There are a small number of issues with self-sizing where it mostly works for the built-in styles but can fail for certain cases.

This is a side-effect of how the built-in styles manage their labels and constraints differently from how it would be done for a custom cell style. If you examine the built-in cell's content view in the debugger, its constraints array is empty.

For one edge case, if one of the built-in labels was not needed for a cell, Apple optimizes it by completely removing that view from the cell. When that cell gets reused and the label needs to be added back in, the constraints don't get updated for that initial occurrence. It was possible to work around that particular issue by ensuring that both labels always have text, so the view never was removed.

You definitely should not need to include heightForRowAtIndexPath. You should remove that, then see if you can work around the built-in style issue within cellForRowAtIndexPath. If not, you may have to use a custom cell which mimics the Left Detail style.

Either way, I'd recommend submitting a bug report with a small sample project that illustrates that "Left Detail" issue, as Apple has fixed many of these issues and built-in style self-sizing has been far more usable.

Community
  • 1
  • 1
  • Thank you so much! I added some code in `cellForRowAtIndexPath` to check how long the text in the detail label would be and set the height to be taller when the text exceeded 100 characters which works. Is there a better way to calculate how tall the height should be? I might just make a custom cell- the only reason I was using the Left Style style in the first place was because it was "easier." – Tessa Feb 17 '16 at 13:43