Skip to content

Instantly share code, notes, and snippets.

@lucianboboc
Last active May 5, 2019 16:07
Show Gist options
  • Save lucianboboc/ce8ea30688f8c47ad7a51180cfc079a6 to your computer and use it in GitHub Desktop.
Save lucianboboc/ce8ea30688f8c47ad7a51180cfc079a6 to your computer and use it in GitHub Desktop.
PAT used as type
class PersonCell: UICollectionViewCell {
@IBOutlet weak var firstLabel: UILabel!
@IBOutlet weak var secondLabel: UILabel!
}
struct Person {
let firstName: String
let lastName: String
}
protocol PersonsDatasourceProtocol: class {
associatedtype Model
associatedtype Cell: UICollectionViewCell
func didSelect(model: Model)
func indexPath(for cell: Cell) -> IndexPath?
func reloadUI()
}
class PersonsDatasource<Model, Cell: UICollectionViewCell>: UICollectionViewDataSource, UICollectionViewDelegate {
var models: [Model] = [] // will be loaded from an api
weak var delegate: PersonsDatasourceProtocol<Model, Cell>? // not possible
typealias CellConfigurator = (Model, Cell) -> Void
private let cellConfigurator: CellConfigurator
init(cellConfigurator: @escaping CellConfigurator) {
self.cellConfigurator = cellConfigurator
}
// datasource and delegate methods
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let model = self.models[indexPath.item]
delegate?.didSelect(model: model)
}
}
class PersonsViewController: UIViewController, PersonsDatasourceProtocol {
typealias Model = Person
typealias Cell = PersonCell
@IBOutlet weak var collectionView: UICollectionView!
let datasource = PersonsDatasource<Model, Cell>(cellConfigurator: { model, cell in
cell.firstLabel.text = model.firstName
cell.secondLabel.text = model.lastName
})
func didSelect(model: Model) {
// do something with the model ...
}
func indexPath(for cell: Cell) -> IndexPath? {
return collectionView.indexPath(for: cell)
}
func reloadUI() {
collectionView.reloadData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment