Question 6
Table and collection view performance
A table or collection view gets sluggish as content grows. What mistakes do you look for, and how do you fix them?
Follow-ups
- Reuse, Auto Layout, prefetch, diffable tradeoffs?
Answer outline
Four areas cause most table and collection view slowdowns:
- 1.Reuse —
prepareForReusemust reset all state; async loads must cancel and re-validate the item ID. Wrong reuse causes stutter and wrong content in cells. - 2.Layout — too many constraints per cell, intrinsic content churn, or self-sizing without cached heights leads to repeated layout passes. Set an estimated row heigh and use the full Auto Layout chain for self-sizing cells.
- 3.Prefetching —
UICollectionViewDataSourcePrefetching/UITableViewDataSourcePrefetchingstarts loads early. Cancel incancelPrefetchingForItemsAtwhen the user scrolls away. - 4.Diffable data source — requires stable IDs so the same item always has the same identifier. Gives animated updates with fewer
performBatchUpdatesbugs, but the model must be diff-friendly and snapshots must stay consistent.
Principles
- Keep
cellForItemcheap — move work towillDisplayor background tasks with a clear lifecycle. - Profile scrolling with Instruments: Core Animation FPS + Time Profiler together.
- Avoid N × expensive Auto Layout in deep hierarchies — flatten or use manual layout for hot cells.
Estimated height (UIKit)
tableView.estimatedRowHeight = 120
tableView.rowHeight = UITableView.automaticDimension
Follow-up angles
- SwiftUI
Listhas the same identity rules — unstableForEachIDs hurt scroll performance just as much. - Compositor-friendly cells use opaque backgrounds and fewer translucent overlays to reduce offscreen rendering.