I have a UICollection view which shows items. Now I want to add pagination on UICollectionView. I do not want to use any third party for this functionality. Please let me know how I can achieve this!
I have four one example http://slicode.com/bottom-refresh-control-uicollectionview/
But in this I have to drag scrollview upwards for few second to make it work.
Setting up for pagination in swift Let's create an Xcode project, add a table view in the Main storyboard. Create TableViewCell and Xib. After that, register TableViewCell and assign delegate and dataSource. Your TableViewCell.
An object that manages an ordered collection of data items and presents them using customizable layouts.
Pagination is a process that is used to divide a large data into smaller discrete pages, and this process is also known as paging. Pagination is commonly used by web applications and can be seen on Google.
If you want to add bottom refresh control you have to use below helper class.
https://github.com/vlasov/CCBottomRefreshControl/tree/master/Classes
Download this 2 files which is in Objective-C. You have to import this file in Bridging header
#import "UIScrollView+BottomRefreshControl.h"
Add refresh control like this.
let refreshControl = UIRefreshControl.init(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
refreshControl.triggerVerticalOffset = 50.0
refreshControl.addTarget(self, action: #selector(paginateMore), for: .valueChanged)
collectionView.bottomRefreshControl = refreshControl
This helper class provide you new bottomRefreshControl
property, which helps you to add refresh control in bottom.
Please try this it works for me.
Step 1:
Declare a global variable :
var totalCount : NSInteger?
var isGetResponse : Bool = true
var activityIndiator : UIActivityIndicatorView?
Set the viewDidLoad:
activityIndiator = UIActivityIndicatorView(frame: CGRect(x: self.view.frame.midX - 15, y: self.view.frame.height - 140, width: 30, height: 30))
activityIndiator?.activityIndicatorViewStyle = .white
activityIndiator?.color = UIColor.black
activityIndiator?.hidesWhenStopped = true
activityIndiator?.backgroundColor = COLOR.COLOR_TABLEVIEW_BACKGROUND
activityIndiator?.layer.cornerRadius = 15
self.view.addSubview(activityIndiator!)
self.view.bringSubview(toFront: activityIndiator!)
Step 2: Set the value in the api call method:
self.totalCount = array.count
self.isGetResponse = true
Step 3: Write this code :
func scrollViewDidScroll(_ scrollView: UIScrollView) {
if isGetResponse {
if (scrollView.contentOffset.y == scrollView.contentSize.height - scrollView.frame.size.height)
{
if totalArrayCount! > self.arrProductListing.count{
isGetResponse = false
activityIndiator?.startAnimating()
self.view.bringSubview(toFront: activityIndiator!)
pageIndex = pageIndex + 1
print(pageIndex)
self.getProductListing(withPageCount: pageIndex)
}
}
//LOAD MORE
// you can also add a isLoading bool value for better dealing :D
}
}
In service call you can send the page index and from the server end they send back the response.
Thank you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With