UITableView dynamic cell heights only correct after some scrolling UITableView dynamic cell heights only correct after some scrolling ios ios

UITableView dynamic cell heights only correct after some scrolling


I don't know this is clearly documented or not, but adding [cell layoutIfNeeded] before returning cell solves your problem.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    TableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"TestCell"];    NSUInteger n1 = firstLabelWordCount[indexPath.row];    NSUInteger n2 = secondLabelWordCount[indexPath.row];    [cell setNumberOfWordsForFirstLabel:n1 secondLabel:n2];    [cell layoutIfNeeded]; // <- added    return cell;}


This worked for me when other similar solutions did not:

override func didMoveToSuperview() {    super.didMoveToSuperview()    layoutIfNeeded()}

This seems like an actual bug since I am very familiar with AutoLayout and how to use UITableViewAutomaticDimension, however I still occasionally come across this issue. I'm glad I finally found something that works as a workaround.


Adding [cell layoutIfNeeded] in cellForRowAtIndexPath does not work for cells that are initially scrolled out-of-view.

Nor does prefacing it with [cell setNeedsLayout].

You still have to scroll certain cells out and back into view for them to resize correctly.

This is pretty frustrating since most devs have Dynamic Type, AutoLayout and Self-Sizing Cells working properly — except for this annoying case. This bug impacts all of my "taller" table view controllers.