Swift 如何在新的NSCollectionView中以编程方式选择对象(并使该对象显示为选定对象)?

Swift 如何在新的NSCollectionView中以编程方式选择对象(并使该对象显示为选定对象)?,swift,macos,nscollectionview,Swift,Macos,Nscollectionview,我已经成功地在我的Mac应用程序中实现了10.11版本的NSCollectionView。它显示我想要的10个项目,但我希望在应用程序启动时自动选择第一个项目 我在viewDidLoad和viewDidAppear函数中尝试了以下操作: let indexPath = NSIndexPath(forItem: 0, inSection: 0) var set = Set<NSIndexPath>() set.insert(indexPath) collectionView.anima

我已经成功地在我的Mac应用程序中实现了10.11版本的NSCollectionView。它显示我想要的10个项目,但我希望在应用程序启动时自动选择第一个项目

我在viewDidLoad和viewDidAppear函数中尝试了以下操作:

let indexPath = NSIndexPath(forItem: 0, inSection: 0)
var set = Set<NSIndexPath>()
set.insert(indexPath)
collectionView.animator().selectItemsAtIndexPaths(set, scrollPosition:   NSCollectionViewScrollPosition.Top)
有无动画师()

虽然它们都在所选索引路径中包含索引路径,但实际上都不会将项目显示为所选


有什么线索表明我哪里出错了吗?

我建议不要使用滚动位置。在Swift 3中,viewDidLoad中的以下代码适用于我

    // select first item of collection view
    collectionView(collectionView, didSelectItemsAt: [IndexPath(item: 0, section: 0)])
    collectionView.selectionIndexPaths.insert(IndexPath(item: 0, section: 0))
第二个代码行是必需的,否则该项永远不会被取消选择。 下面的方法也在起作用

        collectionView.selectItems(at: [IndexPath(item: 0, section: 0)], scrollPosition: NSCollectionViewScrollPosition.top)
对于这两个代码段,都必须使用NSCollectionViewDelegate函数

    func collectionView(_ collectionView: NSCollectionView, didSelectItemsAt indexPaths: Set<IndexPath>) {
    // if you are using more than one selected item, code has to be changed
    guard let indexPath = indexPaths.first
        else { return }
    guard let item = collectionView.item(at: indexPath) as? CollectionViewItem
        else { return }
    item.setHighlight(true)
}
func collectionView(\collectionView:NSCollectionView,didSelectItemsAt indexpath:Set){
//如果使用多个选定项,则必须更改代码
guard let indexPath=indexPaths.first
else{return}
guard let item=collectionView.item(at:indexPath)作为?CollectionViewItem
else{return}
item.setHighlight(true)
}

根据苹果的文档,使用
NSCollectionView
的方法编程选择项目不会调用
NSCollectionViewDelegate
的didSelect方法。所以你必须自己添加亮点

override func viewDidAppear() {
    super.viewDidAppear()

    retainSelection()
}

private func retainSelection() {
    let indexPath = IndexPath(item: 0, section: 0)
    collectionView.selectItems(at: [indexPath], scrollPosition: .nearestVerticalEdge)
    highlightItems(true, atIndexPaths: [indexPath])
}

private func highlightItems(_ selected: Bool, atIndexPaths: Set<IndexPath>) {
    for indexPath in atIndexPaths {
        guard let item = collectionView.item(at: indexPath) else {continue}
        item.view.layer?.backgroundColor = (selected ? NSColor(named: "ItemSelectedColor")?.cgColor : NSColor(named: "ItemColor")?.cgColor)
    }
}
override func viewdide()出现{
super.viewdideappear()
retainSelection()
}
私有函数retainSelection(){
设indexPath=indexPath(项:0,节:0)
collectionView.selectItems(位于:[indexPath],scrollPosition:.nearestVerticalEdge)
highlightItems(true,atindexpath:[indexPath])
}
private func highlightItems(uuSelected:Bool,atindexpath:Set){
对于索引路径中的indexPath{
guard let item=collectionView.item(at:indexPath)else{continue}
item.view.layer?.backgroundColor=(选定的?NSColor(命名为:“ItemSelectedColor”)?.cgColor:NSColor(命名为:“ItemColor”)?.cgColor)
}
}

我想您可以使用view.layer来显示选择状态。看起来NSCollectionViewItem目前还没有图层。如果您将NSCollectionViewItem子类化,请尝试在viewDidLoad方法中启用其视图的WantLayer属性

override func viewDidAppear() {
    super.viewDidLoad()

    self.view.wantsLayer = true
}
您还可以在func collectionView(NSCollectionView,itemForRepresentedObjectAt:IndexPath)->NSCollectionViewItem中启用wantsLayer

func collectionView(collectionView: NSCollectionView, itemForRepresentedObjectAtIndexPath
indexPath: NSIndexPath) -> NSCollectionViewItem {
    let item = self.collectionView.makeItemWithIdentifier("dataSourceItem", forIndexPath: indexPath)

    // Configure the item ...

    item.view.wantsLayer = true

    return item
 }
然后你可以打电话

collectionView.selectionIndexPaths = set

确保它能工作。

对我也不起作用。
collectionView.selectionIndexPaths = set