NSPopUpButtonCell with a hierarchical menu in an NSTableView

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

The menu is 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 select item1 or item2, my data source tableView:setObjectValue:forTableColumn:row: starts, and objectValue is 0 or 1, the index of the selected item. However, when I select an item from one of the submenus, the objectValue that I get is -1. How can I get the selected menu item?

+4
source share
2 answers

I do not think that I rely on -tableView: setObjectValue: forTableColumn: row: when you have a complex set of menus and submenus; if I remember correctly, in fact there is no “object value” for displaying menu items when entering a submenu - NSMenu is too old to use NSIndexPath, as you might expect, and is not complicated enough to try to display integers in the menu and submenu. (In addition, menus tend to change dramatically dynamically, so a value of "3" will be rather distorted.)

Set a goal and action on your NSMenuItems (or perhaps only the parent NSMenus) to do something reliable when everyone is selected.

+1
source

Do you use the same code as for getting elements 1 and 2? I suppose you are, but sometimes these things are easy to overlook = /

0
source

Source: https://habr.com/ru/post/1338143/


All Articles