Created
June 19, 2017 22:11
-
-
Save JohnCoates/46fcb59fcf79392f0c8b731ec1b77cbc to your computer and use it in GitHub Desktop.
UITableView typed cells
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
extension UITableView { | |
func registerCell<CellType: UITableViewCell>(type: CellType.Type) { | |
register(type, forCellReuseIdentifier: String(describing: type)) | |
} | |
func dequeueReusableCell<CellType: UITableViewCell>(for indexPath: IndexPath) -> CellType { | |
let identifier = String(describing: CellType.self) | |
let untypedCell = dequeueReusableCell(withIdentifier: identifier, for: indexPath) | |
guard let cell = untypedCell as? CellType else { | |
let error = "Couldn't cast cell \(untypedCell) " + | |
"with identifier \(identifier) as \(CellType.self)" | |
fatalError(error) | |
} | |
return cell | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: