Last active
October 19, 2019 21:44
-
-
Save descorp/29a6567d1936e4788eff3ea1925eb14e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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