Question

Lets say a Person has many Cars and they have a favorite Car. I cannot seem to find in the docs where it describes the right way to populate the NSPopUpButtonCell in a tableview, differently for each row.

For example, I would have 1 row for each person, the Favorite Car column would have an NSPopUpButtonCell in it. Each row has different menu items. Row 1 (Camaro, Cavalier, F150) Row 2 (745li, Camaro, Town Car) etc...

Was it helpful?

Solution

From the NSTableColumn.h file

Allows per-row customization of the cell used for this NSTableColumn. NSTableView will call -[tableColumn dataCellForRow:] when drawing 'row'. By default this just calls -dataCell. Subclassers can override -dataCellForRow: if they need to potentially use different cells for different rows. On Leopard and higher, the NSTableView delegate method -tableView:dataCellForTableColumn:row: can be used as a convience to avoid subclassing NSTableColumn. In both cases, the returned cell should properly implement -copyWithZone:, since NSTableView may make copies of the cells.

The table will ask the datasource for the selectedIndex in the popupcell, then it will ask the delegate for the menu items. I had my delegate and datasource the same object, lets call it a tableViewController.

When the tableViewController is providing the selectedIndex in the objectValueForTableColumn: (datasource method), the menu doesn't have all the menu times in it, so I have to call the dataCellForRow to get the menu and the index of the selected item in the menu.
The tableViewController implements the dataCellForRow: which creates a new NSPopUpButtonCell and populates it with the menu items.

It works, but seems convoluted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top