You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
3.2 KiB

2 years ago
  1. //
  2. // UICollectionView+ReuseIdentifierProtocol.swift
  3. // R.swift Library
  4. //
  5. // Created by Mathijs Kadijk on 06-12-15.
  6. // From: https://github.com/mac-cain13/R.swift.Library
  7. // License: MIT License
  8. //
  9. import Foundation
  10. import UIKit
  11. public extension UICollectionView {
  12. /**
  13. Returns a typed reusable cell object located by its identifier
  14. - parameter identifier: The R.reuseIdentifier.* value for the specified cell.
  15. - parameter indexPath: The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cells position in the collection view.
  16. - returns: A subclass of UICollectionReusableView or nil if the cast fails.
  17. */
  18. func dequeueReusableCell<Identifier: ReuseIdentifierType>(withReuseIdentifier identifier: Identifier, for indexPath: IndexPath) -> Identifier.ReusableType?
  19. where Identifier.ReusableType: UICollectionReusableView
  20. {
  21. return dequeueReusableCell(withReuseIdentifier: identifier.identifier, for: indexPath) as? Identifier.ReusableType
  22. }
  23. /**
  24. Returns a typed reusable supplementary view located by its identifier and kind.
  25. - parameter elementKind: The kind of supplementary view to retrieve. This value is defined by the layout object.
  26. - parameter identifier: The R.reuseIdentifier.* value for the specified view.
  27. - parameter indexPath: The index path specifying the location of the cell. The data source receives this information when it is asked for the cell and should just pass it along. This method uses the index path to perform additional configuration based on the cells position in the collection view.
  28. - returns: A subclass of UICollectionReusableView or nil if the cast fails.
  29. */
  30. func dequeueReusableSupplementaryView<Identifier: ReuseIdentifierType>(ofKind elementKind: String, withReuseIdentifier identifier: Identifier, for indexPath: IndexPath) -> Identifier.ReusableType?
  31. where Identifier.ReusableType: UICollectionReusableView
  32. {
  33. return dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: identifier.identifier, for: indexPath) as? Identifier.ReusableType
  34. }
  35. /**
  36. Register a R.nib.* for use in creating new collection view cells.
  37. - parameter nibResource: A nib resource (R.nib.*) containing a object of type UICollectionViewCell that has a reuse identifier
  38. */
  39. func register<Resource: NibResourceType & ReuseIdentifierType>(_ nibResource: Resource)
  40. where Resource.ReusableType: UICollectionViewCell
  41. {
  42. register(UINib(resource: nibResource), forCellWithReuseIdentifier: nibResource.identifier)
  43. }
  44. /**
  45. Register a R.nib.* for use in creating supplementary views for the collection view.
  46. - parameter nibResource: A nib resource (R.nib.*) containing a object of type UICollectionReusableView. that has a reuse identifier
  47. */
  48. func register<Resource: NibResourceType & ReuseIdentifierType>(_ nibResource: Resource, forSupplementaryViewOfKind kind: String)
  49. where Resource.ReusableType: UICollectionReusableView
  50. {
  51. register(UINib(resource: nibResource), forSupplementaryViewOfKind: kind, withReuseIdentifier: nibResource.identifier)
  52. }
  53. }