Skip to content

Instantly share code, notes, and snippets.

@feighter09
Created March 28, 2017 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save feighter09/500e2f84f58702faa02956d8e7b01482 to your computer and use it in GitHub Desktop.
Save feighter09/500e2f84f58702faa02956d8e7b01482 to your computer and use it in GitHub Desktop.
Make tableviews a bit safer
protocol CellType {
static var identifier: String { get }
}
extension CellType {
static var identifier: String { return "\(Self.self)" }
}
extension UITableView {
func register<Cell>(forReuse cell: Cell.Type)
where Cell: CellType, Cell: UITableViewCell
{
register(Cell.self, forCellReuseIdentifier: Cell.identifier)
}
func dequeueReusable<Cell>(_ cellType: Cell.Type, for indexPath: IndexPath) -> Cell?
where Cell: CellType, Cell: UITableViewCell
{
return dequeueReusableCell(withIdentifier: Cell.identifier, for: indexPath) as? Cell
}
}
extension StoryCell: CellType {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment