Skip to content

Instantly share code, notes, and snippets.

@descorp
Last active October 19, 2019 21:44
Show Gist options
  • Save descorp/29a6567d1936e4788eff3ea1925eb14e to your computer and use it in GitHub Desktop.
Save descorp/29a6567d1936e4788eff3ea1925eb14e to your computer and use it in GitHub Desktop.
public protocol TableCellGenerator: class {
var identifier: UITableViewCell.Type { get }
var cellHeight: CGFloat { get }
var estimatedCellHeight: CGFloat? { get }
func generate(tableView: UITableView, for indexPath: IndexPath) -> UITableViewCell
func registerCell(in tableView: UITableView)
}
public protocol ViewBuilder {
associatedtype ViewType: UIView
func build(view: ViewType)
}
public extension TableCellGenerator where Self: ViewBuilder {
func generate(tableView: UITableView, for indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: self.identifier.nameOfClass, for: indexPath) as? Self.ViewType else {
return UITableViewCell()
}
self.build(view: cell)
return cell as? UITableViewCell ?? UITableViewCell()
}
func registerCell(in tableView: UITableView) {
tableView.registerNib(self.identifier)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment