Generic Cell Controllers
GenericCellControllers
is a Swift framework that simplifies dealing with [heterogeneous] lists of data in a UITableView
or UICollectionView
. Its main goal is to reduce boilerplate and to let you focus on the important bits of your feature.
Motivation
You can find all about the process that led us to create GenericCellControllers
in our blog.
Features
- Handles all boilerplate letting you focus on your features.
- Uses generics to adapt to your types. No need to downcast in your code.
- Highly flexible and expandable.
- Unified API for UITableView and UICollectionView.
- Compiler time checks. Helps you detect mistakes ahead of time.
How to use
The best way to learn how to use it is to read our blogposts or have a look at the provided example project.
A quick summary would be:
- Subclass
GenericCellController
and implement the methods you require (all of them are optional).
import GenericCellControllers
class MyCellController: GenericCellController<MyCell> {
func configureCell(_ cell: MyCell) {
// Your configuration code...
}
func cellSelected() {
// Action to execute when the cell is selected...
}
}
- Register your cell controllers in your
UITableView
orUICollectionView
.
MyCellController.registerCell(on: tableView)
MyOtherCellController.registerCell(on: tableView)
- Create your list of cell controllers that matches your list of [heterogeneous] data.
import GenericCellControllers
class MyTableController: UITableViewDataSource, UITableViewDelegate {
var tableView: UITableView!
var cellControllers: [TableCellController] = []
...
func updateTableView() {
cellControllers = createCellControllers()
tableView.delegate = self
tableView.dataSource = self
tableView.reloadData()
}
}
For points 2 and 3 we recommend the use of the Factory pattern.
- Forward the relevant delegate and datasource methods to the Cell Controllers.
...
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
return cellControllers[indexPath.row].cellFromReusableCellHolder(tableView, forIndexPath: indexPath)
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
cellControllers[indexPath.row].cellSelected()
}
...
Again, refer to our blog (by now you may have figured out that you should really read it 😜) or have a look at the example project for more details.
IndexPath
s
Variable The GenericCellController
s use the IndexPath
they are sent while dequeuing cells to be able to locate that cell in other methods, like currentCell()
. If you insert or delete cells in your UITableView
or UICollectionView
without reloading it, remember to manually update the IndexPath
of the Cell Controllers that may be affected.
How to install
CocoaPods
- Make sure
use_frameworks!
is added to yourPodfile
. - Include the following in your
Podfile
:
pod 'GenericCellControllers'
- Run
pod install
.
Carthage
Simply add GenericCellControllers to your Cartfile:
github "Busuu/generic-cell-controllers"
Manually
- Clone, add as a submodule or download.
- Drag and drop
GenericCellControllers
project to your workspace. - Add
GenericCellControllers
to Embedded binaries.
License
Source code is distributed under MIT license.