JXSegmentedTitleOrImageDataSource.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. //
  2. // JXSegmentedTitleOrImageDataSource.swift
  3. // JXSegmentedView
  4. //
  5. // Created by jiaxin on 2019/1/22.
  6. // Copyright © 2019 jiaxin. All rights reserved.
  7. //
  8. import UIKit
  9. open class JXSegmentedTitleOrImageDataSource: JXSegmentedTitleDataSource {
  10. /// 数量需要和item的数量保持一致。可以是ImageName或者图片地址。选中时不显示图片就填nil
  11. open var selectedImageInfos: [String?]?
  12. /// 内部默认通过UIImage(named:)加载图片。如果传递的是图片地址或者想自己处理图片加载逻辑,可以通过该闭包处理。
  13. open var loadImageClosure: LoadImageClosure?
  14. /// 图片尺寸
  15. open var imageSize: CGSize = CGSize(width: 30, height: 30)
  16. open override func preferredItemModelInstance() -> JXSegmentedBaseItemModel {
  17. return JXSegmentedTitleOrImageItemModel()
  18. }
  19. open override func reloadData(selectedIndex: Int) {
  20. selectedAnimationDuration = 0.1
  21. super.reloadData(selectedIndex: selectedIndex)
  22. }
  23. open override func preferredRefreshItemModel( _ itemModel: JXSegmentedBaseItemModel, at index: Int, selectedIndex: Int) {
  24. super.preferredRefreshItemModel(itemModel, at: index, selectedIndex: selectedIndex)
  25. guard let itemModel = itemModel as? JXSegmentedTitleOrImageItemModel else {
  26. return
  27. }
  28. itemModel.selectedImageInfo = selectedImageInfos?[index]
  29. itemModel.loadImageClosure = loadImageClosure
  30. itemModel.imageSize = imageSize
  31. }
  32. //MARK: - JXSegmentedViewDataSource
  33. open override func registerCellClass(in segmentedView: JXSegmentedView) {
  34. segmentedView.collectionView.register(JXSegmentedTitleOrImageCell.self, forCellWithReuseIdentifier: "cell")
  35. }
  36. open override func segmentedView(_ segmentedView: JXSegmentedView, cellForItemAt index: Int) -> JXSegmentedBaseCell {
  37. let cell = segmentedView.dequeueReusableCell(withReuseIdentifier: "cell", at: index)
  38. return cell
  39. }
  40. }