【问题标题】:How do you search NSOutlineView for an item that's not visible?您如何在 NSOutlineView 中搜索不可见的项目?
【发布时间】:2019-11-28 21:42:39
【问题描述】:

我有一个通过数据源子类填充的 NSOutlineView。但是我一直在尝试搜索大纲视图中的某个项目。如果项目不可见,outlineView.row(forItem: item) 返回 -1。那么,如何找到不可见的项目?

我可以从数据源中获取项目行,但它仅在可见时才找到它,并在不可见时返回 -1。

let row = outlineView.row(forItem: file)
if row != -1 {
    outlineView.selectRowIndexes(NSIndexSet(index: row) as IndexSet, byExtendingSelection: false)
}

如果项目在大纲视图中可见,则上述代码有效。

一旦你找到不可见的项目,你如何让它可见以便我可以选择它?

【问题讨论】:

  • 第二个问题的答案:使用scrollRowToVisible(_:)
  • 该项目是否因为其父项未展开而不可见?
  • @Willeke -- 好点!我可以从上到下循环并展开每个父级,然后滚动到可见。似乎应该有一种更优雅的方式来做到这一点,但我想这必须这样做。谢谢!

标签: swift xcode macos cocoa


【解决方案1】:

这是我的解决方案:

    private func select(_ indexPath:IndexPath) {
        // Make visible the found item
        //      First, collapse all items
        self.outlineView.collapseItem(nil, collapseChildren: true)
        
        //      Second, start with the root item and start expanding the tree
        var children = self.treeController.arrangedObjects.children
        for point in indexPath {
            if children != nil {
                let item = children![point]
                self.outlineView.expandItem(item)
                children = item.children
            }
        }
        //      third, select the item
        self.treeController.setSelectionIndexPath(indexPath)
    }

您不一定要先折叠所有项目,但我喜欢这个功能。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多