How to get selected item of NSOutlineView without using NSTreeController?
Question
How do I get the selected item of an NSOutlineView with using my own data source. I see I can get selectedRow but it returns a row ID relative to the state of the outline. The only way to do it is to track the expanded collapsed state of the items, but that seems ridiculous.
I was hoping for something like:
array = [outlineViewOutlet selectedItems];
I looked at the other similar questions, they dont seem to answer the question.
Solution
NSOutlineView
inherits from NSTableView
, so you get nice methods such as selectedRow
:
id selectedItem = [outlineView itemAtRow:[outlineView selectedRow]];
OTHER TIPS
@Dave De Long: excellent answer, here is the translation to Swift 3.0
@objc private func onItemClicked() {
if let item = outlineView.item(atRow: outlineView.clickedRow) as? FileSystemItem {
print("selected item url: \(item.fileURL)")
}
}
Shown is a case where item is from class FileSystemItem with a property fileURL.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow