Giter Club home page Giter Club logo

loadmoretableviewcontroller's Introduction

LoadMoreTableViewController

LoadMoreTableViewController is a TableViewController that helps you to show some data like fetched from a web API successively.

Basic Usage

Prepare a Cell

You need to prepare a cell that is displayed on a LoadMoreTableViewController.

  • Xib file way

    1. Create a xib file then put a Table View Cell on it.
    2. Register the cell to the table of the LoadMoreTableViewController with using UITableView method - registerNib:forCellReuseIdentifier:.
      • The cellReuseIdentifier should be the same as the LoadMoreTableViewController property public var cellReuseIdentifier.
  • Storyboard way

    1. On a Storyboard, add a Table View Cell to a Table View Controller that inherits LoadMoreTableViewController.
    2. Set the Identifier of the Table View cell as it is the same as the LoadMoreTableViewController property public var cellReuseIdentifier.

The cell's identifier should be the same as the LoadMoreTableViewController property public var cellReuseIdentifier on the both ways. The default value is "Cell" and it is configureable.

The cell should be designed with using Auto Layout. The LoadMoreTableViewController is using Automatic Dimension feature of UITableView so it will adjust the cell height.

Set Closures

  • public var fetchSourceObjects: (completion: (sourceObjects: [AnyObject], hasNext: Bool) -> ()) -> ()

    • Fetch the new data in this closure.
    • Call completion closure to return these information.
      • The fetched new objects (sourceObjects).
      • If the next loading exists (hasNext).
  • public var configureCell: (cell: UITableViewCell, row: Int) -> UITableViewCell

    • Configure the cell and return it in this closure.
    • The cell type is the same as you prepared.

Example

import LoadMoreTableViewController

class MyTableViewController: LoadMoreTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.registerNib(UINib(nibName: "StandardCell", bundle: nil), forCellReuseIdentifier: cellReuseIdentifier)

        fetchSourceObjects = { [weak self] completion in
            self?.request(offset: sourceObjects.count) { products in
                completion(sourceObjects: products, hasNext: true)
            }
        }
        configureCell = { [weak self] cell, row in
            cell.textLabel?.text = (self?.sourceObjects[row] as? Product)?.title
            return cell
        }
    }

}

See also the example project.

Additional Usage

Data Source

The fetched data are stored to the array public var sourceObjects: [AnyObject]. You can directly access or manipulate this array.

Refreshing Data

Use function public func refreshData(immediately immediately: Bool).

  • immediately: true
    • Immediately makes the tableView empty and starts fetching the data from the first.
    • The loading activity indicator shows on the top.
  • immediately: false
    • Refreshes the tableView after fetching the data from the first.
    • This prevents the loading activity indicator is displayed on the top when UIRefreshControl is used.

Other Settings

  • public func showRetryButton()

    • Changes the loading activity indicator to retry button.
    • When retry button is tapped, next loading starts.
  • public static var retryText: String?

    • Changes retry button text.
  • public static var retryImage: UIImage?

    • Changes retry button image.
  • public var didSelectRow: (Int -> ())?

    • Notifies what row is selected.

Installation

pod "LoadMoreTableViewController"

Author

mishimay, [email protected]

License

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

loadmoretableviewcontroller's People

Contributors

mishimay 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.