Skip to content

Instantly share code, notes, and snippets.

@manmal
Created December 3, 2018 09:38
Show Gist options
  • Save manmal/ca8bd6347d2268514f3dd6b53f6bcff1 to your computer and use it in GitHub Desktop.
Save manmal/ca8bd6347d2268514f3dd6b53f6bcff1 to your computer and use it in GitHub Desktop.
TableViewCellProducer
public protocol TableViewCellDequeueIdentifier: CustomStringConvertible { }
public enum TableViewCellProducer<UITVC: UITableViewCell>: Equatable {
case classAndIdentifier(class: UITVC.Type, identifier: TableViewCellDequeueIdentifier, configure: (UITVC) -> ())
case generator(() -> UITVC)
func cell(tableView: UITableView) -> UITVC {
switch self {
case let .classAndIdentifier(clazz, identifier, configure):
tableView.register(UITVC.self, forCellReuseIdentifier: identifier)
guard let cell = tableView.dequeueReusableCell(withIdentifier: identifier.description) as? UITVC else {
return UITVC()
}
configure(cell)
return cell
case let .generator(generator):
return generator()
}
}
public static func == (lhs: TableViewCellProducer, rhs: TableViewCellProducer) -> Bool {
switch (lhs, rhs) {
case let (.classAndIdentifier(_, cellIdentifierL, _), .classAndIdentifier(_, cellIdentifierR, _)):
return cellIdentifierL.description == cellIdentifierR.description
case (.generator, .generator):
// Weak definition of equality. We assume that we don't need it
// to be stronger. The alternative would be to add some kind of tag
// only for equality purposes.
return true
default:
return false
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment