UICollectionView+Extension.swift 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // UICollectionView+Extension.swift
  3. // QuickSearchLocation
  4. //
  5. // Created by Destiny on 2024/4/16.
  6. //
  7. import UIKit
  8. // MARK: - 二、滚动和注册
  9. public extension UICollectionView {
  10. // MARK: 2.1、是否滚动到顶部
  11. /// 是否滚动到顶部
  12. /// - Parameter animated: 是否要动画
  13. func scrollToTop(animated: Bool) {
  14. setContentOffset(CGPoint(x: 0, y: 0), animated: animated)
  15. }
  16. // MARK: 2.2、是否滚动到底部
  17. /// 是否滚动到底部
  18. /// - Parameter animated: 是否要动画
  19. func scrollToBottom(animated: Bool) {
  20. let y = contentSize.height - frame.size.height
  21. if y < 0 { return }
  22. setContentOffset(CGPoint(x: 0, y: y), animated: animated)
  23. }
  24. // MARK: 2.3、滚动到什么位置(CGPoint)
  25. /// 滚动到什么位置(CGPoint)
  26. /// - Parameter animated: 是否要动画
  27. func scrollToOffset(offsetX: CGFloat = 0, offsetY: CGFloat = 0, animated: Bool) {
  28. setContentOffset(CGPoint(x: offsetX, y: offsetY), animated: animated)
  29. }
  30. // MARK: 2.4、注册自定义cell
  31. /// 注册自定义cell
  32. /// - Parameter cellClass: UICollectionViewCell类型
  33. func register(cellClass: UICollectionViewCell.Type) {
  34. register(cellClass.self, forCellWithReuseIdentifier: cellClass.className)
  35. }
  36. // MARK: 2.5、注册Xib自定义cell
  37. /// 注册Xib自定义cell
  38. /// - Parameter nib: nib description
  39. func register(nib: UINib) {
  40. register(nib, forCellWithReuseIdentifier: nib.className)
  41. }
  42. // MARK: 2.6、创建UICollectionViewCell(注册后使用该方法)
  43. /// 创建UICollectionViewCell(注册后使用该方法)
  44. /// - Parameters:
  45. /// - cellType: UICollectionViewCell类型
  46. /// - indexPath: indexPath description
  47. /// - Returns: 返回UICollectionViewCell类型
  48. func dequeueReusableCell<T: UICollectionViewCell>(cellType: T.Type, cellForRowAt indexPath: IndexPath) -> T {
  49. return dequeueReusableCell(withReuseIdentifier: cellType.className, for: indexPath) as! T
  50. }
  51. // MARK: 2.7、注册自定义: Section 的Header或者Footer
  52. /// 注册自定义: Section 的Header或者Footer
  53. /// - Parameters:
  54. /// - reusableView: UICollectionReusableView类
  55. /// - elementKind: elementKind: header:UICollectionView.elementKindSectionHeader 还是 footer:UICollectionView.elementKindSectionFooter
  56. func registerCollectionReusableView(reusableView: UICollectionReusableView.Type, forSupplementaryViewOfKind elementKind: String) {
  57. register(reusableView.self, forSupplementaryViewOfKind: elementKind, withReuseIdentifier: reusableView.className)
  58. }
  59. // MARK: 2.8、创建Section 的Header或者Footer(注册后使用该方法)
  60. /// 创建Section 的Header或者Footer(注册后使用该方法)
  61. /// - Parameters:
  62. /// - reusableView: UICollectionReusableView类
  63. /// - collectionView: collectionView
  64. /// - elementKind: header:UICollectionView.elementKindSectionHeader 还是 footer:UICollectionView.elementKindSectionFooter
  65. /// - indexPath: indexPath description
  66. /// - Returns: 返回UICollectionReusableView类型
  67. func dequeueReusableSupplementaryView<T: UICollectionReusableView>(reusableView: T.Type, in collectionView: UICollectionView, ofKind elementKind: String, for indexPath: IndexPath) -> T {
  68. return collectionView.dequeueReusableSupplementaryView(ofKind: elementKind, withReuseIdentifier: reusableView.className, for: indexPath) as! T
  69. }
  70. }