Skip to content

Instantly share code, notes, and snippets.

@nahive
Last active December 15, 2017 10:45
Show Gist options
  • Save nahive/5dbc93cd0afb6bfff929b88ef5192f36 to your computer and use it in GitHub Desktop.
Save nahive/5dbc93cd0afb6bfff929b88ef5192f36 to your computer and use it in GitHub Desktop.
protocol CollectionViewType {
func register<T: UICollectionViewCell>(_ cellClass: T.Type)
func dequeue<T: UICollectionViewCell>(_ cellClass: T.Type, for indexPath: IndexPath) -> T?
}
extension UICollectionView: CollectionViewType {
func register<T: UICollectionViewCell>(_ cellClass: T.Type) {
register(cellClass, forCellWithReuseIdentifier: String(describing: cellClass))
}
func dequeue<T: AnyObject>(_ cellClass: T.Type, for indexPath: IndexPath) -> T? {
return dequeueReusableCell(withReuseIdentifier: String(describing: cellClass), for: indexPath) as? T
}
}
// USAGE
let cell = collectionView.dequeue(MediaCell.self, for: indexPath)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment