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.
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
.
For my case,
I had an NSTableView
with items from under the "Items list view",
and an OutlineView
similar to this:
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 useNSTableView *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.
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