// // KMBatchCollectionView.swift // PDF Master // // Created by lizhe on 2023/1/12. // import Cocoa class KMBatchCollectionView: KMBaseXibView { @IBOutlet weak var collectionView: NSCollectionView! var delegate: KMBatchCollectionViewDelegate? var inputType: KMBatchCollectionViewType? { didSet { self.didSelectBatchType(type: inputType!) } } // /** // @abstract 外部传入数据 // @param inputData 文件路劲 // */ // var inputData: [URL]! { // didSet { // self.tableView.inputData = inputData // } // } var presenter: KMBatchCollectionViewPrensenter = KMBatchCollectionViewPrensenter() var data:[KMBatchCollectionViewModel] = [] override func draw(_ dirtyRect: NSRect) { super.draw(dirtyRect) // Drawing code here. } deinit { self.delegate = nil } override func setup() { super.setup() self.contentView.wantsLayer = true self.contentView.layer?.backgroundColor = NSColor.km_init(hex: "#F7F8FA").cgColor self.collectionView.delegate = self self.collectionView.dataSource = self self.collectionView.isSelectable = true self.collectionView.allowsMultipleSelection = false //注册cell self.collectionView.register(KMBatchCollectionViewItem.self, forItemWithIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem")) self.presenter.initPresenter(view: self) } override func reloadData() { super.reloadData() } } extension KMBatchCollectionView: KMBatchCollectionViewPrensenterDelegate { func showData(presenter: KMBatchCollectionViewPrensenter, data: Array) { self.data = data self.collectionView.reloadData() } } extension KMBatchCollectionView: NSCollectionViewDelegate { //当item被选中 public func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set) { for item in self.data { item.isSelect = false } let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem view.model.isSelect = true self.collectionView.reloadData() if self.delegate != nil { self.delegate?.didSelect(index: indexPaths.first!, data: view.model) } } // //当item取消选中 // public func collectionView(_ collectionView: NSCollectionView, didDeselectItemsAt indexPaths: Set) { // let view = collectionView.item(at: indexPaths.first!) as! KMBatchCollectionViewItem // view.model.isSelect = false // self.collectionView.reloadData() // } func didSelectBatchType(type: KMBatchCollectionViewType) { for item in self.data { if item.type == type { item.isSelect = true } else { item.isSelect = false } } self.collectionView.reloadData() } } extension KMBatchCollectionView: NSCollectionViewDataSource { func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return self.data.count } //返回对应的item自定义个体 public func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let view = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "KMBatchCollectionViewItem"), for: indexPath) as! KMBatchCollectionViewItem view.model = self.data[indexPath.item] return view } } extension KMBatchCollectionView: NSCollectionViewDelegateFlowLayout { public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> NSSize { return NSSize(width: self.contentView.bounds.width, height: 32) } public func collectionView(_ collectionView: NSCollectionView, layout collectionViewLayout: NSCollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 16 } } //MARK: KMBatchCollectionViewDelegate protocol KMBatchCollectionViewDelegate: NSObject { func didSelect(index: IndexPath, data: KMBatchCollectionViewModel) }