简体   繁体   中英

How do you search NSOutlineView for an item that's not visible?

I have an NSOutlineView that populates via a datasource subclass. But I'm stuck trying to search the outline view for an item. outlineView.row(forItem: item) returns -1 if the item is not visible. So, how do you find an item that's not visible?

I can get the item row from the data source but it only finds it if it's visible and returns -1 when it's not visible.

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

The above code works if the item is visible in the outlineView.

And once you find the item that's not visible, how do you make it visible so I can select it?

Here is my solution:

    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)
    }

You don't necessarily have to collapse all items first, but I like that functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM