A flexible and easy-to-use pagination framework inspired by Texture batch fetching API.
Pagination
provides an easy-to-use API for implementing infinite scrolling in your applications. It allows seamless integration of pagination functionality in any scrollable view, whether it's a UITableView
, UICollectionView
, or any other scrollable container.
With Pagination
, you can effortlessly manage pagination in your app by automatically detecting when a user has scrolled close to the end of the current content and triggering the fetching of the next page. The framework supports both vertical and horizontal scrolling and is designed to work seamlessly with various UI components.
- Easily integrates with
UIScrollView
,UITableView
, andUICollectionView
. - Supports both
vertical
andhorizontal
scroll directions. - Provides customizable prefetching distance to control when the next batch of data is fetched.
- Objective-C Support: Fully compatible with Objective-C projects, making it easier to integrate into existing codebases.
Implementing infinite scrolling is straightforward, especially with vertical scrolling. Set up the delegate to handle requests for new page prefetching
collectionView.pagination.delegate = self
Implement the delegate method to fetch data for the new page
func pagination(_ pagination: Pagination, prefetchNextPageWith context: PaginationContext) {
// Fetch the next page of data from your source
fetchData(forPage: nextPage) { result in
switch result {
case .success(let data):
// Append the new data and update UI.
pagination.isEnabled = nextPage < data.totalPages
context.finish(true)
case .failure:
// Failed to fetch data
context.finish(false)
}
}
}
Important
It is essential to call context.finish(_:)
once the data loading is complete to accurately update the pagination state.
To disable pagination, set the isEnabled
property to false
. This will stop pagination from monitoring the scrollable view
collectionView.pagination.isEnabled = false
For horizontal scrolling, configure pagination to handle horizontal scroll
collectionView.pagination.direction = .horizontal
To adjust the prefetching distance, set the leadingScreensForPrefetching
property to your desired value. The default is 2
leading screens. Setting it to 0
will stop pagination from notifying you about new data prefetching
collectionView.pagination.leadingScreensForPrefetching = 3
Note
Pagination
is fully compatible with Objective-C projects. Simply import the module and use the provided APIs.
self.tableView.pagination.isEnabled = YES;
self.tableView.pagination.direction = PaginationDirectionVertical;
self.tableView.pagination.leadingScreensForPrefetching = 3;
self.tableView.pagination.delegate = self;
Check out the Example directory to see how to use Pagination in real-world scenarios.
You can add pagination to an Xcode project by adding it as a package dependency.
https://github.com/grighakobian/swift-pagination
If you want to use Pagination in a SwiftPM project, it's as simple as adding it to a dependencies clause in your Package.swift:
dependencies: [
.package(url: "https://github.com/grighakobian/swift-pagination", from: "1.0.0")
]
Paginator is available under the MIT license. See the LICENSE file for more info.