Created
December 3, 2018 09:38
-
-
Save manmal/ca8bd6347d2268514f3dd6b53f6bcff1 to your computer and use it in GitHub Desktop.
TableViewCellProducer
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 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