Swift tableView Pagination
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
Pagination in UITableView keeps scrolling smooth by loading data incrementally instead of downloading everything at once. A robust implementation needs more than a scroll trigger: you also need loading state control, duplicate request prevention, error handling, and end-of-list detection. With a clean architecture, pagination remains stable under fast scrolling and flaky networks.
Choose a Pagination Style
Most APIs use one of two styles:
- page-based, using page number and page size
- cursor-based, using a token returned by previous response
Cursor-based pagination is generally safer for changing datasets because it avoids missing or duplicated records when new items are inserted on the server.
Define Model and Response Types
Keeping the response structure explicit simplifies end-of-list logic.
Manage Pagination State in View Controller
These flags prevent overlapping requests and repeated end-of-list fetches.
Fetch Next Page Safely
A single entry point for pagination requests keeps behavior predictable.
Trigger Loading Near the Bottom
Use willDisplay to fetch before user reaches end.
The threshold gives a prefetch effect and reduces visible loading stalls.
Add a Loading Footer
A footer spinner gives user feedback during requests.
Call this when isLoading changes.
Avoid Duplicate and Out-of-Order Data
When users scroll fast, you may get delayed responses arriving out of order. Protect against stale responses by tracking request tokens or cancelling old tasks. If using URLSession with async code, cancel prior request before starting new one when logic requires strict ordering.
Also deduplicate by id if backend can return overlapping windows.
Support Pull-to-Refresh with Pagination Reset
Refreshing should reset pagination state and request first page again.
Without reset logic, pull-to-refresh can produce inconsistent lists.
Common Pitfalls
- Triggering multiple concurrent requests due to missing
isLoadingguard. - Not handling end-of-list and requesting forever.
- Using exact last-cell trigger, causing visible loading pauses.
- Ignoring duplicate records when backend windows overlap.
- Forgetting to reset pagination state during full refresh.
Summary
- '
UITableViewpagination needs state management, not only a scroll callback.' - Use clear flags for loading state and end-of-list behavior.
- Trigger next-page fetch slightly before the final rows.
- Handle errors, deduplication, and refresh resets explicitly.
- Cursor-based APIs generally provide more reliable pagination under changing data.

