Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add pagination in UICollectionView in swift?

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.

like image 737
TechChain Avatar asked Jul 11 '18 09:07

TechChain


People also ask

How do I Paginate in Swift?

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.

What is swift Uicollectionview?

An object that manages an ordered collection of data items and presents them using customizable layouts.

What is pagination programming?

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.


2 Answers

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.

like image 164
Jaydeep Vora Avatar answered Nov 07 '22 21:11

Jaydeep Vora


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.

like image 44
Sanjukta Avatar answered Nov 07 '22 21:11

Sanjukta