Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get selected item of NSOutlineView without using NSTreeController?

Tags:

cocoa

pyobjc

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.

like image 665
Ronaldo Nascimento Avatar asked Feb 12 '10 16:02

Ronaldo Nascimento


2 Answers

NSOutlineView inherits from NSTableView, so you get nice methods such as selectedRow:

id selectedItem = [outlineView itemAtRow:[outlineView selectedRow]]; 
like image 130
Dave DeLong Avatar answered Sep 23 '22 01:09

Dave DeLong


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){                    }      }  } 
like image 29
M Afham Avatar answered Sep 25 '22 01:09

M Afham