Hi everyone,
I’m currently working on a UITableView setup that uses UITableViewDiffableDataSource. Each cell contains two views:
- A
UILabelfor a title - A
WKWebViewthat loads dynamic HTML content
My Current Approach:
- On initial load, I let all cells render with default height.
- As each
WKWebViewfinishes 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:
- Is this a recommended or sustainable way for handling
WKWebViewinside aUITableViewCell, especially withDiffableDataSource? - Are there any known best practices or alternatives to manage dynamic web content heights more smoothly in table cells?
- 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!