Skip to content

Instantly share code, notes, and snippets.

@pranjalsatija
Last active March 29, 2020 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pranjalsatija/3dc8a7015881b004d7d3c03f6c5daec5 to your computer and use it in GitHub Desktop.
Save pranjalsatija/3dc8a7015881b004d7d3c03f6c5daec5 to your computer and use it in GitHub Desktop.
An example of how to make dequeuing and instantiating UITableViewCells a little nicer.
protocol IdentifiableCell {
static var identifier: String { get }
}
extension IdentifiableCell {
static var identifier: String { String(describing: Self.self) }
}
extension IdentifiableCell where Self: UITableViewCell {
static func dequeue(in tableView: UITableView) -> Self {
let nib = UINib(nibName: identifier, bundle: nil)
tableView.register(nib, forCellReuseIdentifier: identifier)
return tableView.dequeueReusableCell(withIdentifier: identifier) as! Self
}
}
class MyCell: UITableViewCell, IdentifiableCell {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment