6

I want to set my OutlineView's selection programmatically in another class.

I'm able to acces the instance of NSOutlineView by [[appDelegate outlineViewController] outlineView].

The idea behind this, is that I have a view with a list of items (that are also in the outlineview) and I want the user to be able to click on the item in the view, so that the outlineviews's selection is changed and thus a new view (of the selected item) appears.

So again, How can I change OutlineView's selection programmatically?

If the question looks unclear, please let me know what can I do to make it more understandable.

Mikk Rätsep
  • 512
  • 7
  • 20

2 Answers2

9

If I am understanding you correctly, I think you are looking for the NSOutlineView's – selectRowIndexes:byExtendingSelection: method. Note that NSOutlineView is a subclass of NSTableView.

Nate Thorn
  • 2,193
  • 2
  • 23
  • 27
  • It's not working when we use for tree in NSOutlineView. Any way to do selection when using 3-4 level of depth tree ? – Bipin Vayalu Oct 09 '15 at 17:59
2

For my case,
I had an NSTableView with items from under the "Items list view",
and an OutlineView similar to this:

  1. Parent A
    • smth
    • smth
    • smth
    • . . .
  2. Parent B
    • smth
    • smth
    • . . .
  3. Items list view
    • item1
    • item2
    • item3

Parent A and B aren't important, but they are here to demonstrate, that there isn't a fixed number of lines before "Items list view".

So, what I did, was that I used the
- (void)tableViewSelectionDidChange:(NSNotification *)aNotification
method in my NSTableView 's delegate and in the method use
NSTableView *tableView = [aNotification object];
NSInteger clickedRowInTableView = [tableView selectedRow];
to get the new selected row in my tableView.

After that I got the row number of "Items list view" (it dynamically gets it, depending if the "Parents" are expanded or not, and how many children they have), and also expanded the "Items list view", if it wasn't already expanded.

Next, I added all the numbers together: clickedRowInTableView + rowNrOfItemsListView + 1, 1 for the "Items list view" row.

Then I used NSOutlineView's – selectRowIndexes:byExtendingSelection: (thank you Nate, for pointing it out), to set the selected row in OutlineView.

I hope this can be of some help to those in the same situation as I were.

Mikk Rätsep
  • 512
  • 7
  • 20
  • I'm glad you found a solution! If you feel your problem has been fully solved, you should mark either your answer or mine as the accepted answer so that future viewers know that this question is resolved. :) – Nate Thorn Jun 22 '11 at 15:52