Best Practice for Resizing UITableViewCell Containing WKWebView with DiffableDataSource

Hi everyone,

I’m currently working on a UITableView setup that uses UITableViewDiffableDataSource. Each cell contains two views:

  • A UILabel for a title
  • A WKWebView that loads dynamic HTML content

My Current Approach:

  • On initial load, I let all cells render with default height.
  • As each WKWebView finishes loading, I calculate its content height using JavaScript and store this height in a [String: CGFloat] dictionary keyed by a unique identifier for each WebView.
  • I then call tableView.performBatchUpdates to trigger a layout update and resize the cell.
  • When the same cell index and data are shown again, I retrieve the cached height from the dictionary and apply it as a constraint on the WKWebView before loading its content.

This mostly works, but occasionally, there are visual glitches — such as flickering during scrolling or incorrect cell heights temporarily shown before the height is re-applied.

My Questions:

  1. Is this a recommended or sustainable way for handling WKWebView inside a UITableViewCell, especially with DiffableDataSource?
  2. Are there any known best practices or alternatives to manage dynamic web content heights more smoothly in table cells?
  3. Is it possible that the glitches are caused by layout timing issues or reentrancy of height calculation and rendering?

Any suggestions, refinements, or architectural recommendations would be greatly appreciated.

Thanks in advance!

Best Practice for Resizing UITableViewCell Containing WKWebView with DiffableDataSource
 
 
Q