0

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...

joels
  • 7,249
  • 11
  • 53
  • 94
  • Yeah I use bindings. I prefer manual datasources when there is lots of data. – joels Nov 11 '11 at 14:37
  • I have also tried saving an array of menus for each row. This way I don't have to recreate the menus for each row every time I just set the menu for the cell. It still seems slow though. – joels Nov 22 '11 at 18:21

1 Answers1

1

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.

joels
  • 7,249
  • 11
  • 53
  • 94