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.
NSOutlineView
inherits from NSTableView
, so you get nice methods such as selectedRow
:
id selectedItem = [outlineView itemAtRow:[outlineView selectedRow]];
Swift 5
NSOutlineView has a delegate method outlineViewSelectionDidChange
func outlineViewSelectionDidChange(_ notification: Notification) { // Get the outline view from notification object guard let outlineView = notification.object as? NSOutlineView else {return} // Here you can get your selected item using selectedRow if let item = outlineView.item(atRow: outlineView.selectedRow) { } }
Bonus Tip: You can also get the parent item
of the selected item like this:
func outlineViewSelectionDidChange(_ notification: Notification) { // Get the outline view from notification object guard let outlineView = notification.object as? NSOutlineView else {return} // Here you can get your selected item using selectedRow if let item = outlineView.item(atRow: outlineView.selectedRow) { // Get the parent item if let parentItem = outlineView.parent(forItem: item){ } } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With