Skip to content

shabib87/PagingDataController

 
 

Repository files navigation

PagingDataController

[![CI Status](http:https://img.shields.io/travis/NGUYEN CHI CONG/PagingDataController.svg?style=flat)](https://travis-ci.org/NGUYEN CHI CONG/PagingDataController) Version License Platform

Screenshot Pull to refresh Load more data next page

If you find it difficult or uncomfortable when having to write the code again while working with data paging, PagingDataController framework is a solution for you. It's simple and extremely easy to use. Let me help you get started:

S1. Provider: provider is a controller, it will retrieve data by page number

  • Creating a provider.
  • Declare the data model and page size at head.
  • Implement loadData method
class GithubUsersViewControllerProvider: PagingProviderProtocol {
    typealias Model = [String: AnyObject]
    
    var pageSize: Int = 20
    
    func loadData(parameters: [String : AnyObject]?, page: Int, done: (([Model]) -> ())?, finish: ((NSError?) -> ())?) {
        let apiPath = "https://api.github.com/search/users?q=apple&page=\(page+1)&per_page=\(pageSize)"
        Alamofire.request(.GET, apiPath).responseJSON { (response) in
            defer {finish?(nil)}
            let data = response.result.value
            guard data != nil else { return }
            let result = data as! [String: AnyObject]
            done?(result["items"] as! [[String: AnyObject]])
        }
    }
}

S2. Implement view controller conform PagingControllerProtocol protocol

  • Declare provider
    lazy var provider = GithubUsersViewControllerProvider()
  • Setup for paging: default using setupDefaultForPaging method.
    • You can custom more by overriding pagingScrollView getter & return a scroll to display data (by default It auto finds a scrollView in viewController to use as pagingScrollView). Example:
override var pagingScrollView: UIScrollView {
  return self.tableView
}

You can override setupPullToRefreshView & setupInfiniteScrollingView method to trigger a pull action & load more action.

  • Override pullDownAction & infiniteAction to fetch data. Using loadDataPage to get data from provider. This method is implemented by framework.
override func pullDownAction(end: (() -> ())? = nil) {
        loadFirstPage(end)
    }
    
    override func infiniteAction(end: (() -> ())? = nil) {
        loadNextPage(end)
    }
    
    //load data
    func loadData() {
        self.loadDataPage(0, start: { [unowned self] in
            self.showLoading()
        }) { [unowned self] in
            self.pagingScrollView.reloadContent()
            self.hideLoading()
        }
    }
  • All done. Now you only implement a dataSource (table view datasource or collection view data source) to display result. See more details from my example in source code. Good luck!

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Requirements

Installation

PagingDataController is available through CocoaPods. To install it, simply add the following line to your Podfile:

pod "PagingDataController"

Author

NGUYEN CHI CONG, [email protected]

License

PagingDataController is available under the MIT license. See the LICENSE file for more info.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Objective-C 52.6%
  • Swift 45.0%
  • Shell 2.0%
  • Ruby 0.4%