Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set NSOutlineView's selection programmatically (and get NSTableView's selection)

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.

like image 932
Mikk Rätsep Avatar asked Jun 21 '11 14:06

Mikk Rätsep


2 Answers

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.

like image 58
Nate Thorn Avatar answered Nov 10 '22 01:11

Nate Thorn


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.

like image 30
Mikk Rätsep Avatar answered Nov 10 '22 01:11

Mikk Rätsep