UITableViewCell dynamic sizing + size class, cellForRow too soon, willDisplayCell too late?

I am experimenting with UITableViewCell dynamic sizing. I've got a handful of cell prototypes generally working very nicely. Meaning, I'm very happy with this feature. However, I want to adapt several of them to be aware of Compact vs Regular horizontal size class, and the size class will impact the dynamic sizing.


At the time cellForRow is called, the returned cell and its contentView both have traitCollection.horizontalSizeClass of Undefined (sporadically, one appears to be defined, but not before all the visible cells have been initialized and returned), so I am unable to set the font to the appropriate size. If I defer until willDisplayCell, I have great news, the traitColleciton.horizontalSizeClass is set appropriately. However when I change the font size that I want for the respective size class, the cell doesn't change its height, it sticks with the height it determined from cellForRow.


If I:

  • do nothing in cellForRow, the height is the default height
  • use the Compact font size in cellForRow, the height is the Compact size even on Regular size devices (or vice versa)
  • set the font size twice, once in cellForRow (guessing either Compact or Regular) and again in willDisplayCell (with the right answer), the text ends up either clipped or with too much vertical white space (depending on which I guessed) on the other device


In other words, trying to change the font size, or recompute the cell height, from willDisplayCell, will not affect the cell height.I tried adding setNeeds*anything*() in willDisplayCell, but nothing seems to change it from the height it decided on in cellForRow, but I can't seem to get the right size class information in cellForRow.


Am I missing something, or do I need to give up on setting the font size based on size classes in UITableViewCell with dynamic sizing?