KMBatchCollectionView.swift 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. //
  2. // KMBatchCollectionView.swift
  3. // PDF Master
  4. //
  5. // Created by lizhe on 2023/1/12.
  6. //
  7. import Cocoa
  8. class KMBatchCollectionView: KMBaseXibView {
  9. @IBOutlet weak var collectionView: NSCollectionView!
  10. var delegate: KMBatchCollectionViewDelegate?
  11. var inputType: KMBatchCollectionViewType? {
  12. didSet {
  13. self.didSelectBatchType(type: inputType!)
  14. }
  15. }
  16. // /**
  17. // @abstract 外部传入数据
  18. // @param inputData 文件路劲
  19. // */
  20. // var inputData: [URL]! {
  21. // didSet {
  22. // self.tableView.inputData = inputData
  23. // }
  24. // }
  25. var presenter: KMBatchCollectionViewPrensenter = KMBatchCollectionViewPrensenter()
  26. var data:[KMBatchCollectionViewModel] = []
  27. override func draw(_ dirtyRect: NSRect) {
  28. super.draw(dirtyRect)
  29. // Drawing code here.
  30. }
  31. deinit {
  32. self.delegate = nil
  33. }
  34. override func setup() {
  35. super.setup()
  36. self.contentView.wantsLayer = true
  37. self.contentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor
  38. self.collectionView.delegate = self
  39. self.collectionView.dataSource = self
  40. self.collectionView.isSelectable = true
  41. self.collectionView.allowsMultipleSelection = false
  42. //注册cell
  43. self.collectionView.register(KMBatchCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem"))
  44. self.presenter.initPresenter(view: self)
  45. }
  46. override func reloadData() {
  47. super.reloadData()
  48. }
  49. }
  50. extension KMBatchCollectionView: KMBatchCollectionViewPrensenterDelegate {
  51. func showData(presenter: KMBatchCollectionViewPrensenter, data: Array<KMBatchCollectionViewModel>) {
  52. self.data = data
  53. self.collectionView.reloadData()
  54. }
  55. }
  56. extension KMBatchCollectionView: NSCollectionViewDelegate {
  57. //当item被选中
  58. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  59. for item in self.data {
  60. item.isSelect = false
  61. }
  62. let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem
  63. view.model.isSelect = true
  64. self.collectionView.reloadData()
  65. if self.delegate != nil {
  66. self.delegate?.didSelect(index: indexPaths.first!, data: view.model)
  67. }
  68. }
  69. // //当item取消选中
  70. // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  71. // let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem
  72. // view.model.isSelect = false
  73. // self.collectionView.reloadData()
  74. // }
  75. func didSelectBatchType(type: KMBatchCollectionViewType) {
  76. for item in self.data {
  77. if item.type == type {
  78. item.isSelect = true
  79. } else {
  80. item.isSelect = false
  81. }
  82. }
  83. self.collectionView.reloadData()
  84. }
  85. }
  86. extension KMBatchCollectionView: NSCollectionViewDataSource {
  87. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  88. return self.data.count
  89. }
  90. //返回对应的item自定义个体
  91. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  92. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem"), for: indexPath) as! KMBatchCollectionViewItem
  93. view.model = self.data[indexPath.item]
  94. return view
  95. }
  96. }
  97. extension KMBatchCollectionView: NSCollectionViewDelegateFlowLayout {
  98. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  99. return NSSize(width: self.contentView.bounds.width, height: 32)
  100. }
  101. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  102. return 16
  103. }
  104. }
  105. //MARK: KMBatchCollectionViewDelegate
  106. protocol KMBatchCollectionViewDelegate: NSObject {
  107. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel)
  108. }