CollectionViewUtil.swift 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. //
  2. // CollectionViewUtil.swift
  3. // HCQuanfangtong
  4. //
  5. // Created by Apple on 2021/12/29.
  6. // Copyright © 2021 Jyp. All rights reserved.
  7. //
  8. import UIKit
  9. extension UICollectionView{
  10. func register<T>(withType type : T.Type) where T: UICollectionViewCell{
  11. self.register(UINib.loadNib(type), forCellWithReuseIdentifier: String(describing: type))
  12. }
  13. func registerClass<T>(withType type : T.Type) where T: UICollectionViewCell{
  14. self.register(type, forCellWithReuseIdentifier: String(describing: type))
  15. }
  16. func registerHeader<T>(withType type : T.Type) where T: UICollectionReusableView{
  17. self.register(UINib.loadNib(type), forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: type))
  18. }
  19. func registerHeaderClass<T>(withType type : T.Type) where T: UICollectionReusableView{
  20. self.register(type, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: type))
  21. }
  22. func registerFooter<T>(withType type : T.Type) where T: UICollectionReusableView{
  23. self.register(UINib.loadNib(type), forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: type))
  24. }
  25. func registerFooterClass<T>(withType type : T.Type) where T: UICollectionReusableView{
  26. self.register(type, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: type))
  27. }
  28. func dequeueReusableCell<T>(withType type : T.Type,for indexPath : IndexPath) -> T{
  29. return self.dequeueReusableCell(withReuseIdentifier: String(describing: type), for:indexPath) as! T
  30. }
  31. func dequeueReusableSupplementaryHeaderView<T>(withType type : T.Type,for indexPath : IndexPath) -> T{
  32. return self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: String(describing: type), for: indexPath) as! T
  33. }
  34. func dequeueReusableSupplementaryFooterView<T>(withType type : T.Type,for indexPath : IndexPath) -> T{
  35. return self.dequeueReusableSupplementaryView(ofKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: String(describing: type), for: indexPath) as! T
  36. }
  37. }