Git Product home page Git Product logo

pagedlists's Introduction


PagedLists

CI Status Version License Platform Carthage SPM Swift Version

What is it?

PagedLists gives you custom UITableView and UICollectionView classes that supports pagination. All the logic and tracking of loading states, current page and when to actually request next pages is handled by these components.

Features:

  • Customizable number of elements per page.
  • Multiple scrolling directions are supported(i.e: You can load new pages of messages at the top of a chat.)
  • Supports paged scrolling in UICollectionView.
  • Uses delegation to load content from your desired source.

Installation

1. Cocoapods

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

pod 'PagedLists', '~> 1.0.0'

2. Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks. Add the following line to your Cartfile and follow the installation instructions.

github "rootstrap/PagedLists" ~> 1.0.0

3. Swift Package Manager

  • In XCode 11, go to File -> Swift Packages -> Add Package Dependency.
  • Enter the repo URL(https://github.com/rootstrap/PagedLists) and click Next.
  • Select the version rule desired(you can specify a version number, branch or commit) and click Next. This library supports SPM starting from the version 1.0.0.
  • Finally, select the target where you want to use the framework.

That should be it. PagedLists should appear in the navigation panel as a dependency and the framework will be linked automatically to your target.

Note: It is always recommended to lock your external libraries to a specific version.

Usage

You can add a PagedTableView or PagedCollectionView programmatically or via Storyboards by specifying the class in the attributes inspector.

let tableView = PagedTableView(frame: <some CGRect>)

Configuration

Set the number of elements per page you are expecting from your data source.

tableView.elementsPerPage = 20

The pagination delegate

tableView.updateDelegate = self

By conforming to the pagination protocol, PagedTableView and PagedCollectionView gives you an opportunity to load the data from your desired source. You must then send the element count back or the error in case of failure.

extension ViewController: PagedTableViewDelegate {

  func tableView(
    _ tableView: PagedTableView,
    needsDataForPage page: Int,
    completion: (Int, NSError?) -> Void
  ) {
    // Load data from a network request and communicate results
    // back to the PagedTableView
    viewModel.getItems(
      page: page,
      success: { [weak self] newItemsCount in
        tableView.reloadData()
        completion(newItemsCount, nil)
      },
      failure: { [weak self] error in
        self?.showErrorToTheUser()
        completion(0, error)
      }
    )
  }
}
Custom pagination data

In the example above, we are letting PagedTableView to automatically detect if a new page request is necessary based on the number of elements loaded.

If your datasource provides data on pagination you can use that information to control when the table view loads new pages.

tableView.hasMore = requestData.isNextPageAvailable

Restart pagination

You can reset all pagination data on a table or collection view, for example, if you have a pull to refresh control.

dataSource.items = []
tableView.reloadData()
// Resets pagination
tableView.reset()

Example

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

Credits

PagedLists is maintained by Rootstrap and German López with the help of our contributors.

License

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

pagedlists's People

Contributors

glm4 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.