How to know when UITableView did scroll to bottom in iPhone?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Detecting when a UITableView reaches the bottom is a common requirement for infinite scrolling, analytics, and lazy loading. The usual solution is to watch the table view's scroll position and compare the visible bottom edge to the total content height.
The detail that trips people up is that "bottom" is rarely an exact equality check. Content insets, safe area adjustments, bouncing, and dynamic cell heights mean you should trigger with a threshold instead of waiting for a mathematically perfect last pixel.
Detect Bottom in scrollViewDidScroll
Because UITableView is a subclass of UIScrollView, bottom detection is usually implemented in the scroll view delegate.
The threshold lets you start loading before the user hits the absolute end. That usually feels better than waiting until the table is fully exhausted.
Reset Loading State Correctly
The main logic above prevents duplicate requests, but it only works if the loading flag is reset in both success and failure cases.
If you reset the flag only after successful loads, one failed request can leave pagination permanently disabled.
Alternative: Detect the Last Cell in willDisplay
If your goal is specifically "the user is about to see the last row," another clean pattern is tableView(_:willDisplay:forRowAt:).
This is often easier to reason about when your table has discrete pages of rows and you care more about cell display than raw scroll offset.
Handle Short Lists and Empty Tables
Bottom detection behaves differently when the content is shorter than the viewport. In that case, contentSize.height may already be less than the visible height, which means a naive formula will say you are at the bottom as soon as the screen appears.
If that is not what you want, add a guard:
This prevents an immediate extra fetch when the first page contains only a few rows. In other designs, that immediate fetch is desirable, so treat it as a product decision rather than a universal rule.
Consider Insets and Bounce Behavior
Do not base the calculation only on frame.height and contentOffset.y. Modern iPhones often have adjusted content insets, especially with large titles, safe areas, and embedded controllers.
Using adjustedContentInset makes the math line up with what the user can actually see. It also reduces false triggers when the table bounces at the top or bottom.
Common Pitfalls
- Using exact equality such as
offsetY + height == contentHeight, which often fails due to rounding and layout changes. - Forgetting
adjustedContentInset, which can trigger too early or too late on real devices. - Firing pagination from
scrollViewDidScrollwithout anisLoadingguard, causing duplicate network requests. - Resetting the loading flag only on success, leaving the table stuck after one failed request.
- Assuming short content should always trigger more loading immediately, even when the product does not want that behavior.
Summary
- Use
scrollViewDidScrollwhen you want continuous bottom detection for pagination. - Compare visible bottom position to content height with a threshold, not exact equality.
- Use
adjustedContentInsetso the calculation matches the real visible area. - Guard duplicate requests with a loading flag and reset it on success and failure.
- Consider
willDisplayfor a simpler last-cell trigger when that better matches the feature.
Related reading
- How to know XCTestExpectation current fulfillment count
- How to launch an Activity from another Application in Android
- How to launch an Activity from another Application in Android
- How to launch Safari and open URL from iOS app
- How to lay out Views in RelativeLayout programmatically?
- How to lazy load images in ListView in Android
- How to let the app know if it's running Unit tests in a pure Swift project?
- How to let the app know if it's running Unit tests in a pure Swift project?
.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.