KMBatchCollectionView.swift 4.3 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: BaseXibView {
  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. func reloadData() {
  47. }
  48. }
  49. extension KMBatchCollectionView: KMBatchCollectionViewPrensenterDelegate {
  50. func showData(presenter: KMBatchCollectionViewPrensenter, data: Array<KMBatchCollectionViewModel>) {
  51. self.data = data
  52. self.collectionView.reloadData()
  53. }
  54. }
  55. extension KMBatchCollectionView: NSCollectionViewDelegate {
  56. //当item被选中
  57. public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
  58. for item in self.data {
  59. item.isSelect = false
  60. }
  61. let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem
  62. view.model.isSelect = true
  63. self.collectionView.reloadData()
  64. if self.delegate != nil {
  65. self.delegate?.didSelect(index: indexPaths.first!, data: view.model)
  66. }
  67. }
  68. // //当item取消选中
  69. // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set<IndexPath>) {
  70. // let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem
  71. // view.model.isSelect = false
  72. // self.collectionView.reloadData()
  73. // }
  74. func didSelectBatchType(type: KMBatchCollectionViewType) {
  75. for item in self.data {
  76. if item.type == type {
  77. item.isSelect = true
  78. } else {
  79. item.isSelect = false
  80. }
  81. }
  82. self.collectionView.reloadData()
  83. }
  84. }
  85. extension KMBatchCollectionView: NSCollectionViewDataSource {
  86. func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
  87. return self.data.count
  88. }
  89. //返回对应的item自定义个体
  90. public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
  91. let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem"), for: indexPath) as! KMBatchCollectionViewItem
  92. view.model = self.data[indexPath.item]
  93. return view
  94. }
  95. }
  96. extension KMBatchCollectionView: NSCollectionViewDelegateFlowLayout {
  97. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize {
  98. return NSSize(width: self.contentView.bounds.width, height: 32)
  99. }
  100. public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
  101. return 16
  102. }
  103. }
  104. //MARK: KMBatchCollectionViewDelegate
  105. protocol KMBatchCollectionViewDelegate: NSObject {
  106. func didSelect(index: IndexPath, data: KMBatchCollectionViewModel)
  107. }