【问题标题】:Using NSPopUpButtonCell with NSTableView将 NSPopUpButtonCell 与 NSTableView 一起使用
【发布时间】:2010-08-15 01:41:50
【问题描述】:

您好,我正在尝试在 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

【问题讨论】:

  • //设置测试数据\n NSDictionary * dictionary = @{LXDetailItemName:@"one", LXDetailItemDataType:@"NULL", LXDetailItemDevice:@"NULL"};\n for (int i = 0; i

标签: objective-c cocoa nstableview nspopupbuttoncell


【解决方案1】:

我想我已经明白了。单击菜单项后,我使用弹出单元格的方法“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:objectValueForTableColumn:row:”中的弹出单元格返回任何内容。我无法在此方法中为弹出单元格返回任何内容,它仍然可以工作。有cmets吗?
  • 我在 6 年后使用 Swift 3 遇到了同样的问题。似乎在 objectValueFor:tableColumn 内的 NSPopupButtonCell 上设置菜单项不会持续存在,但将其放入 willDisplayCell 会工作。奇怪的是,没有willDisplayCell,文本字段和复选框确实可以正常工作。
【解决方案2】:

我认为在tableView:setObjectValue:forTableColumn:row: 中设置值后,您需要做的就是在表格视图上调用reloadData,以强制它使用您对数据模型所做的更改进行自我更新。 NSPopUpButtonCell 确实使用项目索引作为其对象值,因此在没有 willDisplayCell 委托方法中的代码的情况下,部分事情应该可以正常工作。

顺便说一句,我建议使用菜单项的representedObject 属性来告诉项目部分而不是标题。菜单项标题可能会被本地化,并且使用标题存储数据值很容易损坏。

【讨论】:

  • 我尝试在设置对象值后调用 reloadData,但 NSPopUpButtonCell 不会自行更新。这是关于表示对象的一个​​好点,我将使用它。谢谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-12
  • 2019-06-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多