Search code examples
iosswiftuitableviewcelluitableviewautomaticdimension

Detail Label in "Left Detail" style cell text getting cut off- swift


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?


Solution

  • 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.