Recycle is a swift module for iOS that helps reusing UITableView and UICollectionView cells and views. It's more clear and less verbose than the native counterparts.
If you're tired of spending dozens of lines registering cells and then casting all dequeued ones, this module is for you.
You will improve cell registering:
// From this:
let nibOne = UINib(nibName: "MyFileOne", bundle: self.bundle)
let cellOne = tableView.register(nib, forCellWithReuseIdentifier: "cellOne")
let nibTwo = UINib(nibName: "MyFileTwo", bundle: self.bundle)
let cellTwo = tableView.register(nib, forCellWithReuseIdentifier: "cellTwo")
// To this:
tableview.registerRecyclableCells(CellOne.self, CellTwo.self)And improve cell reuse:
// From this:
let cell = tableView.dequeueReusableCell(withIdentifier: "cellOne") as? CellOne ?? CellOne()
// To this:
let cell = tableView.recycle(CellOne.self)That's plenty of reason to me :)
Get started
UITableViewCell
- Creating a recyclable UITableViewCell
- Registering recyclable UITableViewCell
- Recycling a UITableViewCell
UITableViewHeaderFooterView
- Creating a recyclable UITableViewHeaderFooterView
- Registering recyclable UITableViewHeaderFooterView
- Recycling a UITableViewHeaderFooterView
UICollectionViewCell
- Creating a recyclable UICollectionViewCell
- Registering recyclable UICollectionViewCell
- Recycling a UICollectionViewCell
UICollectionReusableView
- (coming soon)
Configuring recyclable cells and views
Add Recycle as a dependency to your project!
Add this to your Podfile and do a pod install:
pod 'Recycle', '~> 0.0.2'Add this to your cartfile and do a carthage update:
github "mateusnroll/recycle" ~> 0.0.2Recycle extends the UITableView adding registerRecyclableCells and Recycle functions. Both accept cells that conform to the Recyclable protocol.
The cell class should inherint from UITableViewCell and use the Recyclable mixin.
import Recycle
class CellOne: UITableViewCell, Recyclable {
}tableView.registerRecyclableView(cells: Reusable.Type...)
Registering cells should be done when you first load the view, ideally on viewDidLoad. This will avoid trying to recycle something that has not been registered yet. You can register as many cells as you want, using the variadic parameter.
overrid func viewDidLoad() {
super.viewDidLoad()
tableView.registerRecyclableCells(CellOne.self, CellTwo.self, CellThree.self)
// ... More code
}This can be used anywhere, but will probably be used on tableView(_ tableView:, cellForRowAt indexPath:), hence the example. The recycle function will always return the same type it is passed but optional, which means there's no need for casting.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.recycle(CellOne.self)
return cell ?? UITableViewCell()
}Recycle extends the UITableView to add registerRecyclableHeaderFooterViews and recycleHeaderFooter functions. Both accept views that conform to the Recyclable protocol.
The view class should inherint from UITableViewHeaderFooterView and use the Recyclable mixin.
import Recycle
class HeaderOne: UITableViewHeaderFooterView, Recyclable {
}tableView.registerRecyclableHeaderFooterViews(views: Reusable.Type...)
Registering views should be done when you first load the cotroller view, ideally on viewDidLoad. You can register as many views as you want, using the variadic parameter.
overrid func viewDidLoad() {
super.viewDidLoad()
tableView.registerRecyclableHeaderFooterViews(HeaderOne.self, HeaderTwo.self, HeaderThree.self)
// ... More code
}This can be used anywhere, but will probably be used on tableView(_ tableView:, viewForHeaderInSection section:), hence the example. The recycle function will always return the same type it is passed but optional, which means there's no need for casting.
override func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
return tableView.recycleHeaderFooter(HeaderOne.self)
}Recycle extends the UICollectionView to add registerRecyclableCells and recycle functions. Both accept cells that conform to the Recyclable protocol.
The view class should inherint from UICollectionViewCell and use the Recyclable mixin.
import Recycle
class CollectionCellOne: UICollectionViewCell, Recyclable {
}collectionView.registerRecyclableCells(cells: Reusable.Type...)
Registering cells should be done when you first load the view, ideally on viewDidLoad. You can register as many cells as you want, using the variadic parameter.
overrid func viewDidLoad() {
super.viewDidLoad()
tableView.registerRecyclableCells(CollectionCellOne.self, CollectionCellTwo.self, CollectionCellThree.self)
// ... More code
}This can be used anywhere, but will probably be used on collectionView(_ collectionView:, cellForItemAt indexPath:), hence the example. The recycle function will always return the same type it is passed but optional, which means there's no need for casting.
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.recycle(CollectionCellOne.self, for: indexPath)
return cell ?? UICollectionViewCell()
}All cells and views that conform to the Recyclable protocol will have some properties inferred. You can take a look at them and maybe overwrite some. The demos below are for UITableViewCell, but it can be applied to any of the supported types.
To use it with a nib, just create it with a file name equal to the class (CellOne.xib), and add the class as the cell's custom class. Cells that don't have a xib will be configured without it.
To configure a custom xib filename, set the class' nibName property. Don't add the .xib extension.
import Recycle
class CellOne: UITableViewCell, Recyclable {
static var nibName: String? = "MyNibFilename"
}To configure a custom bundle, se the class' bundle property. The default is nil.
import Recycle
class CellOne: UITableViewCell, Recyclabe {
static var bundle: Bundle? = Bundle(for: CellOne.self)
}The cell's identifier will be the its class name as a string. To configure a custom one, set the class' identifier property.
import Recycle
class CellOne: UITableViewCell, Recyclabe {
static var identifier: String = "MyCustomIdentifier"
}