Skip to content

Instantly share code, notes, and snippets.

@thiagolioy
Created November 21, 2016 15:29
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 thiagolioy/e7510e091acf4533bc9a2237c5a28981 to your computer and use it in GitHub Desktop.
Save thiagolioy/e7510e091acf4533bc9a2237c5a28981 to your computer and use it in GitHub Desktop.
protocol ReusableTableViewCell {
}
extension ReusableTableViewCell where Self: UITableViewCell {
static func cellIdentifier() -> String {
return String(describing: Self.self)
}
static func registerForTableView(_ tableView: UITableView) {
let nib = UINib(nibName: cellIdentifier(), bundle: nil)
tableView.register(nib, forCellReuseIdentifier: cellIdentifier())
}
static func dequeueCell(from tableView: UITableView, at indexPath: IndexPath) -> Self {
if let cell = tableView
.dequeueReusableCell(withIdentifier: cellIdentifier(), for: indexPath) as? Self {
return cell
} else {
return Self()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment