How to detect the end of loading of UITableView
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Detecting when a UITableView has finished loading all its cells is needed for tasks like scrolling to a specific row, running animations, taking snapshots, or measuring layout. The challenge is that UITableView loads cells lazily and reloadData() returns immediately before the cells are rendered. There is no built-in delegate method for "loading complete," but several reliable techniques exist.
Method 1: layoutIfNeeded() After reloadData()
Force a synchronous layout after reloading:
layoutIfNeeded() forces the table view to lay out all visible cells immediately. This is the simplest approach but blocks the main thread until layout completes.
Method 2: Dispatch to Next Run Loop
Use DispatchQueue.main.async to execute code after the current layout cycle:
This works because reloadData() schedules layout on the current run loop, and DispatchQueue.main.async executes after that layout pass completes.
Method 3: CATransaction Completion Block
Wrap reloadData() in a CATransaction to get a completion callback:
This is the most reliable approach — CATransaction captures all implicit animations triggered by reloadData() and calls the completion block when they finish.
Method 4: willDisplay / didEndDisplaying
Track cell display events to detect when loading completes:
A more practical approach — detect when the last row is displayed:
Method 5: Custom ReloadData with Completion
Create a UITableView extension that provides a completion handler:
This extension is the cleanest API for handling post-reload actions.
Method 6: performBatchUpdates Completion
For targeted updates (insert, delete, reload sections):
performBatchUpdates provides a built-in completion handler — no workarounds needed.
Method 7: KVO on contentSize
Observe the table view's content size changes:
This fires multiple times during loading. Debounce or check for stability if you need a single "done" signal.
Detecting End of Pagination Loading
For infinite scroll / load-more patterns:
Common Pitfalls
reloadData()is asynchronous: It returns immediately but cells are not rendered yet. Never access cell properties or scroll right afterreloadData()without using one of the techniques above.willDisplayfires for recycled cells:willDisplayis called every time a cell becomes visible, not just during initial load. Guard against duplicate logic if using this for one-time setup.- Multiple
reloadData()calls: Rapid consecutivereloadData()calls may cause the completion block to fire for an intermediate state. Debounce or cancel previous operations. - Empty table views: If the data source returns 0 rows,
willDisplayis never called. Handle the empty state separately. - Background thread:
reloadData()and all UIKit calls must happen on the main thread. Calling from a background thread causes undefined behavior or crashes.
Summary
- Use
CATransactionwith a completion block for the most reliable post-reload callback - Use the
UITableViewextensionreloadData(completion:)for a clean API - Use
DispatchQueue.main.asyncafterreloadData()for a simple one-liner - Use
performBatchUpdatesfor insert/delete operations — it has a built-in completion handler - Use
willDisplaydelegate to detect when the last visible cell is rendered - Never access cells or scroll immediately after
reloadData()— cells are not rendered yet
Related reading
- How to detect the end of UISlider drag?
- How to detect when a TextField loses the focus in SwiftUI for iOS?
- How to detect when a UIScrollView has finished scrolling
- How to detect when an Android app goes to the background and come back to the foreground
- How to detect when AVPlayer video ends playing?
- How to detect when keyboard is shown and hidden
- How to determine height of UICollectionView with FlowLayout
- How to determine if an NSDate is today?
.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.