This is an old question, but the situation is still the same. As there was a request for a swift version, here's my take. I didn't find the accepted answer to work for me as I think you should interact directly with your data source class rather than extending the NSOutlineView. Annoyingly, the outline won't find rows unless they are expanded which is why it's simpler to use your dataSource. In my case I found that you have to expand the parent's of your items in reverse order, so that you start at the top level and work your way towards the actual item you're trying to expand. I feel like that should be built in, but unless I missed something - it's not.
In this example FileItem is my data source collection item class. It contains a property "parent" which needs to be valid if it is part of the hierarchy displayed.
func selectItem(_ item: FileItem, byExtendingSelection: Bool = false) {
guard let outlineView = outlineView else { return }
var itemIndex: Int = outlineView.row(forItem: item)
if itemIndex < 0 {
var parent: FileItem? = item
var parents = [FileItem?]()
while parent != nil {
parents.append(parent)
parent = parent?.parent
}
let reversedTree = parents.compactMap({$0}).reversed()
for level in reversedTree {
outlineView.expandItem(level, expandChildren: false)
}
itemIndex = outlineView.row(forItem: item)
if itemIndex < 0 {
print("Didn't find", item)
return
}
}
print("Expanding row", itemIndex)
outlineView.selectRowIndexes(IndexSet(integer: itemIndex), byExtendingSelection: byExtendingSelection)
outlineView.scrollRowToVisible(itemIndex)
}