Jerky Scrolling After Updating UITableViewCell in place with UITableViewAutomaticDimension
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Jerky scrolling in UITableView with self-sizing cells (UITableView.automaticDimension) happens when the table view's estimated row height does not match the actual height, causing the table to recalculate content offsets during scrolling. The primary fix is to cache cell heights after they are displayed and return the cached value from estimatedHeightForRowAt. This prevents the table view from guessing heights, which eliminates the jumping and stuttering that occurs when estimated and actual heights differ significantly.
The Problem
The table view uses estimated heights to calculate scroll indicators and content size. When the actual height differs from the estimate, the table adjusts its content offset, producing visible jerks.
Fix 1: Cache Cell Heights
This is the most effective fix. After a cell is displayed, its actual height is cached. On subsequent layout passes, the cached value is returned as the estimate, preventing any height mismatch.
Fix 2: Better Default Estimates
If caching is not feasible, providing estimates that closely match actual heights reduces the severity of jumps.
Fix 3: Use beginUpdates/endUpdates for In-Place Updates
beginUpdates()/endUpdates() tells the table view to recalculate heights for visible cells without reloading them. This avoids the jarring reload animation.
Fix 4: Disable Automatic Prefetching
Prefetching calculates cell sizes for off-screen rows using estimated heights, which can trigger additional content offset adjustments when the actual sizes differ.
Fix 5: Use performBatchUpdates Instead
performBatchUpdates groups insertions, deletions, and reloads into a single animation pass, reducing layout thrashing.
Fix 6: Prevent Content Offset Jumps
This brute-force approach saves the scroll position before the update and restores it after, preventing any visible jump.
Comprehensive Solution
Common Pitfalls
- Using a single
estimatedRowHeightfor all cells: A single estimate like44causes massive jumps when actual heights vary (e.g., 200-point image cells). ImplementestimatedHeightForRowAtwith per-cell or cached heights for accurate estimates. - Calling
reloadData()instead of targeted updates:reloadData()invalidates all cached heights and recalculates the entire table, causing visible jumps. UsereloadRows(at:with:)orbeginUpdates/endUpdatesto update specific cells. - Forgetting to invalidate the height cache after data changes: If cell content changes (longer text, added image), the cached height is stale. Remove the cache entry for the updated row before triggering a layout pass.
- Using
.automaticanimation for in-place updates: The.automaticanimation style adds a fade/slide animation to reloaded cells, which can look like a flicker. Use.nonewhen updating cell content in place to avoid visual artifacts. - Not setting constraints correctly in self-sizing cells: Self-sizing cells require an unbroken chain of constraints from the cell's contentView top to bottom. Missing or ambiguous constraints cause the auto-layout engine to compute incorrect heights, leading to jumps when the correct height is determined later.
Summary
- Cache cell heights in
willDisplayand return them fromestimatedHeightForRowAtto prevent height mismatch jumps - Use
beginUpdates()/endUpdates()to update visible cells without reloading - Provide per-section or per-type height estimates instead of a single
estimatedRowHeight - Use
.noneanimation when reloading rows in place to avoid visual flickering - Invalidate cached heights when cell content changes
- Ensure self-sizing cells have an unbroken top-to-bottom constraint chain in the contentView
Related reading
- JSON Array iteration in Android/Java
- Kafka Producer on Android
- Keep TensorFlow Model Encrypted on Android
- Keeping a UIButton selected after a touch
- Keeping the contentOffset in a UICollectionView while rotating Interface Orientation
- Keras deep learning model to android
- Key hash for Android-Facebook app
- Kotlin-android unresolved reference databinding
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.