Table / Collection DataSource generic wrapper
Model
struct TitleCellModel {
let title: String
let color: UIColor
let action: (IndexPath) -> Void
}
Cell
import G
import UIKit
final class TitleTableCell: UITableViewCell {
var gtcModel: GTCellModel<TitleTableCell>? // <- GTCSetupable.swift
@objc final func tapAction() {
gtcModel?.model.action(gtcModel!.indexPath)
}
}
// MARK: - GTCSetupable
extension TitleTableCell: GTCSetupable {
typealias Model = TitleCellModel
static func createSelf() -> TitleTableCell {
return TitleTableCell()
}
// setup(gtcModel: GTCellModel<Self>) call this method by default. See `GTCSetupable`
func setup(model: TitleCellModel) {
}
func size(in rect: CGRect, model: TitleCellModel) -> CGSize {
return CGSize(width: rect.width, height: 70)
}
}
Creating cell models and push to grid manager. Grid manager itself have a reference on UITableView / UICollectionView and accept delegate / other events.
func createCell(..
let action: (IndexPath) -> Void = { [weak self] indexPath in
...
}
return indexes.map { (index) -> GTCellModel<TitleTableCell> in
let model = TitleCellModel(title: text " \(index)", color: randomColor, action: action)
return TitleTableCell.build(model: model)
}
) -> GTCellModel<TitleTableCell>
let cells = self.createCells(text: "Created at index", indexes: Array(0..<10))
let section = GridSection(header: nil, items: cells, footer: nil)
self.view?.gridManager.reloadData(section: section, animator: .fade())