将 NSPopUpButtonCell 与 NSTableView 一起使用
您好,我正在尝试在 NSTableView 中使用 NSPopUpButtonCell 。基本上,当您在弹出窗口中选择一个项目时,我希望它显示在表视图列/行中。当按下弹出单元格中的某个项目时,我使用“tableView:setObject:forTableColumn:row”将其存储在数据源中,然后当表请求数据时,我检索并在“tableView:objectValueForTableColumn:”中设置弹出单元格的状态排:”。请找到附件我的代码。我现在完全陷入困境。我希望有人能够理解它。 先感谢您。
这是在控制器内部:
//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
这是在数据源/委托类内部。
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
Hello I am trying to use a NSPopUpButtonCell inside an NSTableView. Basically when you select an item in the pop up I want it displayed in the table view column/row. When an item in the popup cell is pressed I store it inside the data source using "tableView:setObject:forTableColumn:row", then when the table requests data I retrieve and and set the state of the popup cell in "tableView:objectValueForTableColumn:row:". Please find attached my code. I am completely stuck right now. I hope someone can make sense of it.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想我已经明白了。我使用弹出单元格的方法“setTitle:”在单击菜单项后更新单元格的文本。这是我使用的表视图委托方法。
I think I've figured it out. I use the method "setTitle:" of the pop up cell to update the text of the cell after the menu item is clicked. Here is the table view delegate method that I used.
我认为在
tableView:setObjectValue:forTableColumn:row:
中设置值后您需要做的就是在表视图上调用reloadData
以强制其自行更新您对数据模型所做的更改。 NSPopUpButtonCell 确实使用项目索引作为其对象值,因此在 willDisplayCell 委托方法中没有该代码的情况下,部分内容应该可以正常工作。顺便说一句,我建议使用菜单项的
representedObject
属性来告诉项目部分而不是标题。菜单项标题可能会被本地化,并且使用标题来存储数据值很容易被破坏。I think all you should need to do after setting the value in
tableView:setObjectValue:forTableColumn:row:
is to callreloadData
on the table view to force it to update itself with the changes you made to the data model. NSPopUpButtonCell does use the item index as its object value, so that part of things should work correctly without that code in the willDisplayCell delegate method.As an aside, I would recommend using the
representedObject
property of a menu item to tell the items part rather than the title. Menu item titles can potentially be localized, and using titles to store data values is very susceptible to breakage.