Skip to content

Instantly share code, notes, and snippets.

@antoniocasero
Created May 5, 2017 14:02
Show Gist options
  • Save antoniocasero/22b3d15294c0ef318e5deba5200b1e2d to your computer and use it in GitHub Desktop.
Save antoniocasero/22b3d15294c0ef318e5deba5200b1e2d to your computer and use it in GitHub Desktop.
Protocol to reuse and simplify UITableViewCell
import Foundation
import UIKit
protocol Reusable {}
extension Reusable where Self: UITableViewCell {
static var reuseIdentifier: String {
return String(describing: self)
}
}
extension UITableViewCell: Reusable {}
extension UITableView {
func register<T: UITableViewCell>(_ :T.Type) where T: Reusable {
register(T.self, forCellReuseIdentifier: T.reuseIdentifier)
}
func dequeueReusableCell<T: UITableViewCell>(forIndexPath indexPath: IndexPath) -> T where T: Reusable {
guard let cell = dequeueReusableCell(withIdentifier: T.reuseIdentifier, for: indexPath) as? T else {
fatalError("Could not deque cell")
}
return cell
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment