Skip to content

Instantly share code, notes, and snippets.

@luca-bernardi
Last active August 9, 2016 12:53
Show Gist options
  • Save luca-bernardi/6d922e77df4a1a9584b49300df65f9cb to your computer and use it in GitHub Desktop.
Save luca-bernardi/6d922e77df4a1a9584b49300df65f9cb to your computer and use it in GitHub Desktop.
protocol Reusable{
static var reuseIdentifier: String { get }
}
extension Reusable {
static var reuseIdentifier: String { return String(Self.self) }
}
extension UICollectionViewCell : Reusable {}
extension UICollectionView {
func register<T: UICollectionViewCell where T: Reusable>(_ : T.Type) {
register(T.self, forCellWithReuseIdentifier: T.reuseIdentifier)
}
func dequeue<T: UICollectionViewCell where T:Reusable>(_: T.Type, for indexPath: IndexPath) -> T {
return dequeueReusableCell(withReuseIdentifier: T.reuseIdentifier, for: indexPath) as! T;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment