GithubHelp home page GithubHelp logo

skladek / sktableviewdatasource Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 1.0 18.75 MB

An easy to configure data source for UITableView.

License: MIT License

Swift 97.55% Ruby 1.75% Objective-C 0.70%
uitableviewdatasource

sktableviewdatasource's Introduction

SKTableViewDataSource

Travis Status Codecov Status Pod Version Platform Status License Status

SKTableViewDataSource provides an object to handle much of the standard UITableViewDataSource logic. It handles calculating row and section counts, retrieving cells, and provides methods for updating the underlying array powering the data source. Check out the SampleProject in the workspace to see some usage examples.


Installation

Cocoapods

Instalation is supported through Cocoapods. Add the following to your pod file for the target where you would like to use SKTableViewDataSource:

pod 'SKTableViewDataSource'

Initialization

Auto Cell Registration

The easiest way to initialize a TableViewDataSource object is to provide an array, cell class or nib, and a CellPresenter closure to handle styling the cell during cellForRowAtIndexPath. Cell registration will be handled by the TableViewDataSource object. The objects array can be a 1 or 2 dimensional array. A single dimension array will display as a single section table view. A 2 dimensional array will display with multiple sections.

import SKTableViewDataSource
let dataSource = TableViewDataSource(objects: array, cell: UITableViewCell.self, cellPresenter: { (cell, object) in
    cell.textLabel?.text = object
})

tableview.dataSource = dataSource

Manual Cell Registration

If you require access to the cell's reuse identifier or require multiple cell types in your table view, you can choose to register the cells yourself.

import SKTableViewDataSource
tableView.register(YourCellClass.self, forCellReuseIdentifier: "YourReuseIdentifier")

let dataSource = TableViewDataSource(objects: array, delegate: self) {
    cell.textLabel?.text = object.rawValue
})

tableView.dataSource = dataSource

Note: If you choose to handle cell registration on your own, you must implement TableViewDataSourceDelegate's cellForRowAtIndexPath method and return a cell for each index path.


CellPresenter

Each initialization method for auto cell registration has an optional CellPresenter closure. The closure returns two parameters: a cell and an object. This closure can be used to populate the cell with values from the object.


TableViewDataSourceDelegate

TableViewDataSource has an optional delegate. This serves as a pass through for UITableViewDataSource methods. The delegate object can override any of the TableViewDataSource implementations by implementing the corresponding delegate method.


Updating The Data Array

There are a handful of methods for manipulating the data in the array. Updating the data source will not trigger any sort of update in the table view. That must be handled by the developer.

deleteObjectAt(_:)

This will delete the object at the provided index path.

indexPathOf(_:) -> IndexPath?

This will return the index path of the input object.

insert(object:at:)

This will insert the provided object at the provided index path.

moveObjectFrom(_:to:)

This will move the object at the from index path to the to index path.

objectAt(_:) -> T

This returns the object at the provided index path.

setObjects(_:)

This replaces the existing objects array with the provided objects array.

sktableviewdatasource's People

Contributors

skladek avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Forkers

carabina

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.