Created
May 5, 2017 14:02
-
-
Save antoniocasero/22b3d15294c0ef318e5deba5200b1e2d to your computer and use it in GitHub Desktop.
Protocol to reuse and simplify UITableViewCell
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
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