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.
Add following line to your Podfile and then run pod install:
pod 'CellKit', '~> 0.8'
Optionally you can add DiffableCellKit subspec:
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:
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.
Protocol for your cell model. Use this procotol to specify your cell configuration such as height, xib location, whether the cell is highlightable, etc.
struct PrototypeCellViewModel {
let name: String
}
extension PrototypeCellViewModel: CellModel {
var usesNib: Bool { false }
var registersLazily: Bool { false }
}
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:
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.
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.
class XIBCell: UITableViewCell, CellConfigurable {
@IBOutlet private weak var label: UILabel!
func configure(with model: XIBCellViewModel) {
label.text = "\(model.name)"
}
}
struct XIBCellViewModel: CellConvertible, DifferentiableCellModel, Equatable {
let name: String
// MARK: DifferentiableCellModel
var domainIdentifier: Int
// MARK: CellConvertible
typealias Cell = XIBCell
}
CellKit
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.Optionally you can add
DiffableCellKit
.CocoaPods
Add following line to your
Podfile
and then runpod install
:Optionally you can add
DiffableCellKit
subspec: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
andGenericCellModelSection
, which define yourUITableView
/UICollectionView
cell structure. You can always subclassCellModelDataSource
and override its methods to suit your needs.Here’s an example of typical CellKit data source usage:
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.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.
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 ifusesNib
is false. | AnUINib
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
andreuseIdentifier
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:
DiffableCellKit
DiffableCellKit is an extension build on top of
CellKit
andDifferenceKit
which captures your data source changes and automatically updates/removes/inserts yourUITableView
/UICollectionView
cells.DifferentiableCellModelDataSource
DifferentiableCellModelDataSource
is built on top of the same foundation asCellModelDataSource
with the difference (no pun intended), that it acceptsDifferentiableCellModelSection
and when you change the content of itssections
property, the data source will issue an animated update to itsUITableView
/UICollectionView
.DifferentiableCellModelDataSource
is still an open class, so you can subclass it and override its methods and propertes to suit your needs.DifferentiableCellModel
Just like
CellModel
,DifferentiableCellModel
is a protocol for your cell model.DifferentiableCellModel
provides one newdomainIdentifier
property and ahasEqualContent(with:)
method which provides enough information forDiffableCellKit
to recognize changes and issueUITableView
/UICollectionView
update.When your cell model conforms to
Equatable
protocol,DiffableCellKit
provides anEquatable
extension, so you don’t have to implementhasEqualContent(with:)
method. DifferentiableCellModel can still be combined withCellConvertible
protocol.CellKit Examples
XIB cell
Storyboard prototype cell
Cell defined in code
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.