Using NSPopUpButtonCell with NSTableView

Hello, I am trying to use NSPopUpButtonCell inside an NSTableView. Basically, when you select an item in a popup, I want it to appear in the column / row of the table. When the item in the popup cell is clicked, I store it inside the data source using "tableView: setObject: forTableColumn: row", then when the table requests the data I retrieve and sets the state of the popup cell in "tableView: objectValueForTableColumn: row:" . Attach my code. I'm completely stuck right now. Hope someone can figure this out. Thank you in advance.

This is inside the controller:

//Create the table columns NSTableColumn *nameColumn = [[NSTableColumn alloc] initWithIdentifier:LXDetailItemName]; NSTableColumn *dataTypeColumn = [[NSTableColumn alloc] initWithIdentifier:LXDetailItemDataType]; NSTableColumn *deviceColumn = [[NSTableColumn alloc] initWithIdentifier:LXDetailItemDevice]; //Data type column drop down NSPopUpButtonCell *dataTypeDropDownCell = [[NSPopUpButtonCell alloc] initTextCell:@"" pullsDown:YES]; [dataTypeDropDownCell setBordered:NO]; [dataTypeDropDownCell setEditable:YES]; NSArray *dataTypeNames = [NSArray arrayWithObjects:@"NULL", @"String", @"Money", @"Date", @"Int", nil]; [dataTypeDropDownCell addItemsWithTitles:dataTypeNames]; [dataTypeColumn setDataCell:dataTypeDropDownCell]; //Add the columns to the table [tableView addTableColumn:nameColumn]; [tableView addTableColumn:dataTypeColumn]; [tableView addTableColumn:deviceColumn]; enter code here 

This is inside the datasource / delegate class.

 enter code here @implementation LXTestDataSource - (id)init { self = [super init]; if (self) { tableContents = [[NSMutableArray alloc] init]; //Setup test data NSMutableArray *keys = [NSMutableArray arrayWithObjects:LXDetailItemName, LXDetailItemDataType, LXDetailItemDevice, nil]; NSMutableArray *objects = [NSMutableArray arrayWithObjects:@"one", @"NULL", @"NULL", nil]; for (int i = 0; i < 4; i++) { NSMutableDictionary *dictionary = [[NSMutableDictionary alloc] initWithObjects:objects forKeys:keys]; [tableContents addObject:dictionary]; [dictionary release]; } } return self; } - (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView { return [tableContents count]; } - (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { if ([[aTableColumn identifier] isEqualToString:LXDetailItemDataType]) { NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; NSString *title = [rowDictionary objectForKey:LXDetailItemDataType]; NSLog(@"objectValueForTableColumn: %@", title); //DEBUG return [NSNumber numberWithInt:[[aTableColumn dataCell] indexOfItemWithTitle:title]]; } else if ([[aTableColumn identifier] isEqualToString:LXDetailItemDevice]) { NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; NSString *title = [rowDictionary objectForKey:LXDetailItemDevice]; NSLog(@"objectValueForTableColumn: %@", title); //DEBUG return [NSNumber numberWithInt:[[aTableColumn dataCell] indexOfItemWithTitle:title]]; } else { NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; return [rowDictionary objectForKey:[aTableColumn identifier]]; } } - (void)tableView:(NSTableView *)aTableView setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { if ([[aTableColumn identifier] isEqualToString:LXDetailItemDataType]) { NSMenuItem *menuItem = [[aTableColumn dataCell] itemAtIndex:[anObject integerValue]]; NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; NSLog(@"%@", [menuItem title]); //DEBUG //Update the object value at the column index [rowDictionary setObject:[menuItem title] forKey:LXDetailItemDataType]; } else if ([[aTableColumn identifier] isEqualToString:LXDetailItemDevice]) { NSMenuItem *menuItem = [[aTableColumn dataCell] itemAtIndex:[anObject integerValue]]; NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; NSLog(@"%@", [menuItem title]); //DEBUG //Update the object value at the column index [rowDictionary setObject:[menuItem title] forKey:LXDetailItemDevice]; } else { //Get the row NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; //Update the object value at the column index [rowDictionary setObject:anObject forKey:[aTableColumn identifier]]; } } @end 
+6
objective-c cocoa nstableview nspopupbuttoncell
source share
2 answers

I think I get it. I use the "setTitle:" method of the popup cell to update the cell text after clicking on a menu item. Here is the table delegate method that I used.

 - (void)tableView:(NSTableView *)aTableView willDisplayCell:(id)aCell forTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex { if ([[aTableColumn identifier] isEqualToString:LXDetailItemDataType]) { NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; NSString *title = [rowDictionary objectForKey:LXDetailItemDataType]; [aCell setTitle:title]; } else if ([[aTableColumn identifier] isEqualToString:LXDetailItemDevice]) { NSMutableDictionary *rowDictionary = [tableContents objectAtIndex:rowIndex]; NSString *title = [rowDictionary objectForKey:LXDetailItemDevice]; [aCell setTitle:title]; } } 
+3
source share

I think that all you need to do after setting the value in tableView:setObjectValue:forTableColumn:row: is to call reloadData in the table view to make it update itself with the changes you made to the data model. NSPopUpButtonCell uses the element index as its object value, so some things should work correctly without this code in the willDisplayCell delegate method.

As an aside, I would recommend using the representedObject property of a menu item to indicate part of the items, not the title. The names of menu items can be localized, and the use of captions to store data is very sensitive to breakage.

+2
source share

All Articles