我的NSCollectionView项目集成了另一个NSCollectionView。一切正常,但在第一个视图中进行项目选择时却无法。
当我设置collectionView1.isSelectable = true
时,仅当我单击第二个(集成)collectionView以外的其他位置时,才会调用didSelectItemsAt
委托。更清楚地说,如果我单击标签,图像或自定义视图,则可以使用。当我单击第二个集合视图时,不会调用该委托。
这是我到目前为止尝试过的:
collectionView2.isSelectable = true
设置为第二个视图。那没有用。我尚未在iOS上使用UICollectionView和单元格选择进行尝试,但我倾向于认为问题是相同的。
编辑:
因此,如果我单击绿色标签或蓝色部分的任何地方(这是第一个集合视图项目),则将正确调用didSelectItemsAt
委托。如果我单击橙色的NSView(内部有第二个CollectionView),则不会调用它。
这里是第一个CollectionView的简化代码:
class DashboardVC: NSViewController { @IBOutlet weak var collectionView1: NSCollectionView! override func viewDidLoad() { super.viewDidLoad() self.initViews() } fileprivate func initViews() { let flowLayout = NSCollectionViewFlowLayout() flowLayout.itemSize = NSSize(width: 400.0, height: 380.0) flowLayout.sectionInset = NSEdgeInsets(top: 20.0, left: 20.0, bottom: 20.0, right: 20.0) flowLayout.minimumInteritemSpacing = 20.0 flowLayout.minimumLineSpacing = 20.0 self.collectionView1.collectionViewLayout = flowLayout self.collectionView1.isSelectable = true self.collectionView1.dataSource = self self.collectionView1.delegate = self } } extension DashboardVC: NSCollectionViewDataSource { func numberOfSections(in collectionView: NSCollectionView) -> Int { return 1 } func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int { return 5 } func collectionView(_ itemForRepresentedObjectAtcollectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem { let item = self.collectionView1.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "CollectionViewItem"), for: indexPath) guard let collectionViewItem = item as? CollectionViewItem else { return item } return collectionViewItem } } extension DashboardVC: NSCollectionViewDelegate { func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) { print("I can't make this work!") } }
对于第二个集合视图:
class TabularView: NSView {
lazy var collectionView2: NSCollectionView = {
let gridLayout = NSCollectionViewGridLayout()
gridLayout.maximumNumberOfColumns = self.numberOfColums
gridLayout.maximumNumberOfRows = self.numberOfRows
let collectionView = NSCollectionView(frame: .zero)
collectionView.collectionViewLayout = gridLayout
collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.isSelectable = true //-> does not work :(
collectionView.dataSource = self
collectionView.delegate = self
return collectionView
}()
override init(frame frameRect: NSRect) {
super.init(frame: frameRect)
self.setup()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
self.setup()
}
private func setup() {
self.addSubview(self.collectionView)
NSLayoutConstraint.activate([
self.topAnchor.constraint(equalTo: self.collectionView.topAnchor),
self.bottomAnchor.constraint(equalTo: self.collectionView.bottomAnchor),
self.leadingAnchor.constraint(equalTo: self.collectionView.leadingAnchor),
self.trailingAnchor.constraint(equalTo: self.collectionView.trailingAnchor),
])
}
}
extension TabularView: NSCollectionViewDataSource {
func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
return self.numberOfRows * self.numberOfColums
}
func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
let item = collectionView.makeItem(withIdentifier: NSUserInterfaceItemIdentifier(rawValue: "TabularCollectionViewItem"), for: indexPath)
guard let collectionViewItem = item as? TabularCollectionViewItem else { return item }
return collectionViewItem
}
}
我的NSCollectionView项目集成了另一个NSCollectionView。一切都很好,但是在第一个视图中进行项目选择时却没有。当我设置collectionView1.isSelectable = true时,...
如果我正确理解了您的问题,您正试图禁用对collectionView1
中特定项目的选择,而是让用户选择此单元格中嵌入的collectionView2
项目。