将nspopupupbuttoncell与nstableView一起使用
-
28-09-2019 - |
题
您好,我试图在NstableView内使用Nspopupuptoncell。基本上,当您在弹出窗口中选择一个项目时,我希望它显示在表视图列/行中。按下弹出窗口中的项目时,我使用“ 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
解决方案
我想我已经弄清楚了。我使用弹出式单元格的方法“ settitle:”,在单击菜单项后更新单元格的文本。这是我使用的表视图委托法。
- (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];
}
}
其他提示
我认为在设置值之后,您应需要做的一切 tableView:setObjectValue:forTableColumn:row:
是打电话 reloadData
在表视图上,迫使其对您对数据模型的更改进行更新。 nspopupupbuttoncell确实使用该项目索引作为其对象值,因此,一部分应该在WillDisPlayCell委托方法中正确工作,而无需该代码。
顺便说一句,我建议使用 representedObject
菜单项的属性要告诉项目部分而不是标题。菜单项标题可能会进行本地化,并且使用标题存储数据值非常容易破裂。
不隶属于 StackOverflow