项目作者: futuredapp

项目描述 :
Table View and Collection View data source wrapper
高级语言: Swift
项目地址: git://github.com/futuredapp/CellKit.git
创建时间: 2017-05-02T14:47:14Z
项目社区:https://github.com/futuredapp/CellKit

开源协议:MIT License

下载


CellKit icon

CellKit

Cocoapods
Cocoapods platforms
License

CellKit is a Swift package that streamlines the workflow with cells in UITableView and UICollectionView. No more registering cell classes or XIBs, no more dequeueing cells and setting its view models. CellKit handles this all.

Installation

Swift Package

Add following line to your swift package dependencies, or in Xcode, go to File -> Swift Packages -> Add Package Dependency and type in URL address of this repository.

  1. .package(url: "https://github.com/futuredapp/CellKit", from: "0.8.1")

Optionally you can add DiffableCellKit.

CocoaPods

Add following line to your Podfile and then run pod install:

  1. pod 'CellKit', '~> 0.8'

Optionally you can add DiffableCellKit subspec:

  1. pod 'CellKit', '~> 0.8', subspecs: ['Diffable']

Usage

CellKit provides a data source and a section model which you fill with your cells, headers and footer models. All you’re left to do is to define your cell view and your cell model with protocol conformance to CellConvertible and CellConfigurable and CellKit will handle the rest.

1. Set a DataSource

CellKit provides a CellModelDataSource and GenericCellModelSection, which define your UITableView/UICollectionView cell structure. You can always subclass CellModelDataSource and override its methods to suit your needs.
Here’s an example of typical CellKit data source usage:

  1. let dataSource = CellModelDataSource([
  2. GenericCellModelSection(arrayLiteral:
  3. PrototypeCellViewModel(name: "Prototype")
  4. ),
  5. GenericCellModelSection(arrayLiteral:
  6. CodeCellViewModel(name: "Code")
  7. ),
  8. GenericCellModelSection(arrayLiteral:
  9. XIBCellViewModel(name: "XIB")
  10. )
  11. ])
  12. tableView.dataSource = dataSource
  13. collectionView.dataSource = dataSource

2. Create your cell and CellModel

CellKit support wide variety of cell declarations including .xib files , UITableView interface builder prototype cells and Cells written entirely in code.
Please note that your .xib file, Cell subclass and Cell identifier have to to have the same name. It is possible to not use the same identifier, but it is not recommended.
Pro tip: You can use our custom teplates to generate Cell with CellKit protocols in just a few clicks.

3. Conform to CellKit protocols

In order for your cells and cell view models to work with CellKit, they have to conform to these protocols:

CellConfigurable

This is a protocol for your Cell. This protocol provides a configure(model:) method which gets call when a tableview request a reusable cell and is used to distribute your model to your cells.

  1. class XIBCell: UITableViewCell {
  2. @IBOutlet private weak var label: UILabel!
  3. }
  4. extension XIBCell: CellConfigurable {
  5. func configure(with model: XIBCellViewModel) {
  6. label.text = model.name
  7. }
  8. }

CellModel

Protocol for your cell model. Use this procotol to specify your cell configuration such as height, xib location, whether the cell is highlightable, etc.

  1. struct PrototypeCellViewModel {
  2. let name: String
  3. }
  4. extension PrototypeCellViewModel: CellModel {
  5. var usesNib: Bool { false }
  6. var registersLazily: Bool { false }
  7. }

Here is a handy table of configurable properties and their default values in which you can provide your own values.
| properties | datatype | default | description |
|—————————-|—————|—————————————————————————————|——————————————————————————————————————-|
| registersLazily | Bool | true | Indicates whether DataSource should register a view to its presenting view. |
| usesNib | Bool | true | Indicates whether cell is defined in .xib file |
| nib | UINib? | xib with cellClass name. Or nil if usesNib is false. | An UINib reference of your .xib file containing you view |
| cellClass | AnyClass | | A class reference to views class. |
| reuseIdentifier | String | | a unique re-use identifier |
| cellHeight | Double | 44.0 | hight for cell |
| highlighting | Bool | true | Indicates whether cell can be highlighted |
| separatorIsHidden | Bool | false | Indicates whether should hide separator |

CellConvertible

This protocol extends CellModel with associated type and thus can provide a default cellClass and reuseIdentifier value based on type’s name.
It’s espetially handy when you declare your cell in XIB, because all you need to do is to define its associated type and CellKit will provide the rest.
Here’s an example:

  1. class XIBCell: UITableViewCell, CellConfigurable {
  2. @IBOutlet private weak var label: UILabel!
  3. func configure(with model: XIBCellViewModel) {
  4. label.text = "\(model.name)"
  5. }
  6. }
  7. struct XIBCellViewModel: CellConvertible, CellModel {
  8. let name: String
  9. // MARK: CellConvertible
  10. typealias Cell = XIBCell
  11. }

DiffableCellKit

DiffableCellKit is an extension build on top of CellKit and DifferenceKit which captures your data source changes and automatically updates/removes/inserts your UITableView/UICollectionView cells.

DifferentiableCellModelDataSource

DifferentiableCellModelDataSource is built on top of the same foundation as CellModelDataSource with the difference (no pun intended), that it accepts DifferentiableCellModelSection and when you change the content of its sections property, the data source will issue an animated update to its UITableView/UICollectionView.
DifferentiableCellModelDataSource is still an open class, so you can subclass it and override its methods and propertes to suit your needs.

  1. let datasource = DifferentiableCellModelDataSource(self.tableView, sections: [
  2. DifferentiableCellModelSection(arrayLiteral:
  3. PrototypeCellViewModel(domainIdentifier: 1, name: "Prototype")
  4. ),
  5. DifferentiableCellModelSection(arrayLiteral:
  6. CodeCellViewModel(domainIdentifier: 2, name: "Code")
  7. ),
  8. DifferentiableCellModelSection(arrayLiteral:
  9. XIBCellViewModel(domainIdentifier: 3, name: "XIB")
  10. )
  11. ])

DifferentiableCellModel

Just like CellModel, DifferentiableCellModel is a protocol for your cell model. DifferentiableCellModel provides one new domainIdentifier property and a hasEqualContent(with:) method which provides enough information for DiffableCellKit to recognize changes and issue UITableView/UICollectionView update.
When your cell model conforms to Equatable protocol, DiffableCellKit provides an Equatable extension, so you don’t have to implement hasEqualContent(with:) method.
DifferentiableCellModel can still be combined with CellConvertible protocol.

  1. class XIBCell: UITableViewCell, CellConfigurable {
  2. @IBOutlet private weak var label: UILabel!
  3. func configure(with model: XIBCellViewModel) {
  4. label.text = "\(model.name)"
  5. }
  6. }
  7. struct XIBCellViewModel: CellConvertible, DifferentiableCellModel, Equatable {
  8. let name: String
  9. // MARK: DifferentiableCellModel
  10. var domainIdentifier: Int
  11. // MARK: CellConvertible
  12. typealias Cell = XIBCell
  13. }

CellKit Examples

XIB cell

  1. class XIBCell: UITableViewCell, CellConfigurable {
  2. @IBOutlet private weak var label: UILabel!
  3. func configure(with model: XIBCellViewModel) {
  4. label.text = "\(model.name)"
  5. }
  6. }
  7. struct XIBCellViewModel: CellConvertible, CellModel {
  8. let name: String
  9. // MARK: CellConvertible
  10. typealias Cell = XIBCell
  11. }

Storyboard prototype cell

  1. class PrototypeCell: UITableViewCell, CellConfigurable {
  2. @IBOutlet private weak var label: UILabel!
  3. func configure(with model: PrototypeCellViewModel) {
  4. label.text = "\(model.name)"
  5. }
  6. }
  7. struct PrototypeCellViewModel: CellConvertible, CellModel {
  8. let name: String
  9. // MARK: CellConvertible
  10. typealias Cell = PrototypeCell
  11. let usesNib: Bool = false
  12. let registersLazily: Bool = false
  13. }

Cell defined in code

  1. class CodeCell: UITableViewCell, CellConfigurable {
  2. let label: UILabel = UILabel()
  3. override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
  4. super.init(style: style, reuseIdentifier: reuseIdentifier)
  5. addSubview(label)
  6. label.translatesAutoresizingMaskIntoConstraints = false
  7. NSLayoutConstraint.activate([
  8. topAnchor.constraint(equalTo: label.topAnchor, constant: -16),
  9. bottomAnchor.constraint(equalTo: label.bottomAnchor, constant: 16),
  10. leftAnchor.constraint(equalTo: label.leftAnchor, constant: -16),
  11. heightAnchor.constraint(equalToConstant: 64)
  12. ])
  13. }
  14. func configure(with model: CodeCellViewModel) {
  15. label.text = "\(model.name)"
  16. }
  17. }
  18. struct CodeCellViewModel: CellConvertible, CellModel {
  19. let name: String
  20. // MARK: CellConvertible
  21. typealias Cell = CodeCell
  22. let usesNib: Bool = false
  23. let cellHeight: Double = 64
  24. }

Contributors

Current maintainer and main contributor is Matěj Kašpar Jirásek, matej.jirasek@futured.app.

We want to thank other contributors, namely:

License

CellKit is available under the MIT license. See the LICENSE for more information.