NSPopUpButtonCell with hierarchical menu in NSTableView

638 views Asked by At

I have an NSTableView of which one column contains NSPopUpButtonCells. This column is assigned to an IBOutlet. My application's controller class creates a menu in it's init and assigns it to the NSTableColumn's dataCell, which is the NSPopUpButtonCell.

The menu looks as follows:

NSPopUpButton
    NSMenu
        NSMenuItem item1
        NSMenuItem item2
        NSMenuItem item3
            NSMenu
                NSMenuItem item3_1
                NSMenuItem item3_2
        NSMenuItem item4
                NSMenuItem item4_1
                NSMenuItem item4_2
                NSMenuItem item4_3

When I click the menu and choose item1 or item2, my data source's tableView:setObjectValue:forTableColumn:row: is fired and the objectValue is 0 or 1, the index of the chosen item. However, when I pick an item from one of the submenus, the objectValue I get is -1. How would I be able to get the menu item which was chosen?

2

There are 2 answers

0
Wil Shipley On BEST ANSWER

I don't think I'd rely on -tableView:setObjectValue:forTableColumn:row: when you have a complex set of menus and submenus; if I recall correctly there isn't really an 'object value' to menu item mapping when submenus come into play -- NSMenu is too old to use NSIndexPath like you might hope, and isn't complex enough to try to map integers to menus and submenus. (Plus, menus tend to change a lot dynamically, so the meaning of "3" would be pretty squirmy.)

Set a target and action on your NSMenuItems (or possibly just the parent NSMenus) to do something reliable when each is selected.

0
Koh Jing Yu On

Are you using the same code as you are to retrieve item 1 and 2? I suppose you are, but sometimes these things are easily overlooked =/