首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何在新NSCollectionView中以编程方式选择对象(并使该对象显示为选中)?

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

Stack Overflow用户
提问于 2016-02-05 01:14:23
回答 3查看 2.5K关注 0票数 4

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

我已经在viewDidLoad和viewDidAppear函数中尝试了以下内容;

代码语言:javascript
运行
复制
let indexPath = NSIndexPath(forItem: 0, inSection: 0)
var set = Set<NSIndexPath>()
set.insert(indexPath)
collectionView.animator().selectItemsAtIndexPaths(set, scrollPosition:   NSCollectionViewScrollPosition.Top)

我已经尝试了上面的第四行,有没有动画师

我还尝试了下面的代码来代替第4行

代码语言:javascript
运行
复制
collectionView.animator().selectionIndexPaths = set

使用和不使用动画师()

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

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

EN

回答 3

Stack Overflow用户

发布于 2017-03-08 05:40:53

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

代码语言:javascript
运行
复制
    // select first item of collection view
    collectionView(collectionView, didSelectItemsAt: [IndexPath(item: 0, section: 0)])
    collectionView.selectionIndexPaths.insert(IndexPath(item: 0, section: 0))

第二个代码行是必需的,否则永远不会取消选择该项。下面的方法也是有效的

代码语言:javascript
运行
复制
        collectionView.selectItems(at: [IndexPath(item: 0, section: 0)], scrollPosition: NSCollectionViewScrollPosition.top)

对于这两个代码片段,都必须有一个带有函数的NSCollectionViewDelegate

代码语言:javascript
运行
复制
    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)
}
票数 3
EN

Stack Overflow用户

发布于 2019-12-16 11:58:18

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

代码语言:javascript
运行
复制
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)
    }
}
票数 1
EN

Stack Overflow用户

发布于 2020-05-09 00:06:08

我认为您可以使用view.layer来显示选择状态。而且看起来NSCollectionViewItem目前还没有分层。如果您创建了NSCollectionViewItem子类,请尝试在viewDidLoad方法中启用其视图的wantsLayer属性。

代码语言:javascript
运行
复制
override func viewDidAppear() {
    super.viewDidLoad()

    self.view.wantsLayer = true
}

您也可以在func collectionView(NSCollectionView,itemForRepresentedObjectAt: IndexPath) -> NSCollectionViewItem中启用wantsLayer。

代码语言:javascript
运行
复制
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
 }

然后你就可以调用

代码语言:javascript
运行
复制
collectionView.selectionIndexPaths = set

并确保它能正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35207364

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档