UICollectionView+Identifiable.swift 961 B

123456789101112131415161718192021222324252627
  1. //
  2. // UICollectionView+Identifiable.swift
  3. // Bio
  4. //
  5. // Created by Joan Disho on 22.07.18.
  6. // Copyright © 2018 Joan Disho. All rights reserved.
  7. //
  8. import UIKit
  9. extension UICollectionView {
  10. func register<C: UICollectionViewCell>(cellType: C.Type) where C: ClassIdentifiable {
  11. register(cellType.self, forCellWithReuseIdentifier: cellType.reuseId)
  12. }
  13. func register<C: UICollectionViewCell>(cellType: C.Type) where C: NibIdentifiable & ClassIdentifiable {
  14. register(cellType.nib, forCellWithReuseIdentifier: cellType.reuseId)
  15. }
  16. func dequeueReusableCell<C: UICollectionViewCell>(withCellType type: C.Type = C.self, forIndexPath indexPath: IndexPath) -> C where C: ClassIdentifiable {
  17. guard let cell = dequeueReusableCell(withReuseIdentifier: type.reuseId, for: indexPath) as? C
  18. else { fatalError("Couldn't dequeue a UICollectionViewCell with identifier: \(type.reuseId)") }
  19. return cell
  20. }
  21. }