TestsTested | ✓ |
LangLanguage | SwiftSwift |
License | MIT |
ReleasedLast Release | Sep 2016 |
SPMSupports SPM | ✗ |
Maintained by Oren Rosenblum.
AutoCellFactory
is based on Model-View-ViewModel (MVVM)
architacture, which basically means that the cell owns a ViewModel
(called a presenter
), which holds the model, and when the AutoCellFactory
sets the model, the presenter automatically calls reloadView
and triggers the configureView
at the Cell. A flappy diagram will look like this:
In the configureView
, the cell configures itself by asking the presenter the fields it needs.
To run the example project, clone the repo, and run pod install
from the Example directory first.
AutoCellFactory is available through CocoaPods. To install it, simply add the following line to your Podfile:
pod "AutoCellFactory"
Example for presenting UITableViewCell
with Model
by a Presenter
You’ll need to add the following object:
ACBasePresenter<[YOUR MODEL TYPE]>
struct SomeModel {
var name: String = ""
}
Create a presenter for the cell, subclass of ACBasePresenter
, with a generic type as your model. In this case - SomeModel
.
The presenter is the only one that will know about the model, and will give the cell the configuration based on this model. Its superclass will take care for automatic reloading the view when the model is set
It must include required init()
, so ACBasicCell
, which owns the presenter, could automatically initial it, without involving the subclass
class SomeCellPresenter: ACBasePresenter<SomeModel> {
required init() {}
}
Create a subclass of ACBasicCell
, with a generic type as your presenter. In this case - SomeCellPresenter
.
class SomeCell: ACBasicCell<SomeCellPresenter> {
override func configureCell() {
// Do some configuration for your cell
}
}
The generic type will promise that the superclass will init the presenter with this type, and will trigger configureCell
automatically when the model is set
Be sure you create a nib file for that cell, named exactly like the cell, and set the reuse identifier in the same name
Now its only left to actually show the cell using the AutoCellFactory
Register
create an instance of AutoCellFactory:
lazy var acFactory: AutoCellFactory = AutoCellFactory(tableView: self.tableView, delegate: self.viewModel)
The delegate needs to conforms to AutoCellFactoryViewDelegate
, and to implement the method:
func modelForIndexPath(indexPath: NSIndexPath) -> Any?
Call register method at your AutoCellFactory
instance:
public func registerForNib(cellsAndModels: [AutoCellFactoryRegistrationType])
Pass the tableView, and an array of AutoCellFactoryRegistrationType
public typealias AutoCellFactoryRegistrationType = (cellType: ACBasicCellPresenterHolder.Type, modelType: Any.Type)
This way the factory knows which model belongs to each cell, for example:
acFactory.registerForNib([(SomeCell.self, SomeModel.self)])
And in cellForRow
, just return the cell from the acFactory:
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
return acFactory.getCell(forIndexPath: indexPath)
}
That’s it
Oren Rosenblum, [email protected]
AutoCellFactory is available under the MIT license. See the LICENSE file for more info.