Reputation: 2666
I have a NSOutlineView
and also created a menu that shows up as context menu when I right-click on any element.
Now I have 2 problems.
Upvotes: 1
Views: 236
Reputation: 18253
The clickedRow
method (inherited from NSTableView
) will give you the row number.
The row number can then be transformed to the item using NSOutlineView
's itemForRow
.
For the menu part, the NSMenuDelegate
method menu:updateItem:atIndex:shouldCancel:
is called just before the menu is shown, so you can modify it as needed.
According to the docs, you also have to implement numberOfItemsInMenu:
.
You can set the same object as delegate for both the outline view and the menu.
Upvotes: 1
Reputation: 889
In theory, when a row is right-clicked, it should be already the selected row in the NSOutlineView. Anyway, this does not happen normally.
I have solved this, by implementing the following method (that also answers to the second question):
- (void)menuNeedsUpdate:(NSMenu *)menu
Insert this method in one of your classes and register that class as the menu delegate (please note: the NSMenu, not the single NSMenuItem). In the above code, I have added this piece of code, to automatically select the clicked row when is right clicked.
if ([[arrayController selectedObjects] count] == 0 || [[arrayController selectedObjects] count] == 1) {
if ([myTable clickedRow] != -1) {
[myTable selectRowIndexes:[NSIndexSet indexSetWithIndex:[myTable clickedRow]] byExtendingSelection:FALSE];
}
}
As far as I know, this works correctly inside an NSTableView, so it should work also in your NSOutlineView.
Regarding the second question, you can use the above method to get notified every time the NSMenu is going to be displayed.
Upvotes: 0