代码之家  ›  专栏  ›  技术社区  ›  David

将NSPopUpButtonCell与NSTableView一起使用

  •  6
  • David  · 技术社区  · 16 年前

    你好,我正在尝试使用NSTableView中的NSPopUpButtonCell。基本上,当您在弹出窗口中选择一个项目时,我希望它显示在表视图的列/行中。当弹出单元格中的一个项目被按下时,我使用“tableView:设置对象:福塔列:行,则当表请求数据时,我检索并设置中弹出单元格的状态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
    

    这在datasource/delegate类中。

    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
    
    2 回复  |  直到 16 年前
        1
  •  3
  •   David    16 年前

    我想我已经弄明白了。我使用弹出单元格的方法“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];
        }
    }
    
        2
  •  2
  •   Brian Webster    16 年前

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

    representedObject 一个菜单项的属性,用于指示该项的部分而不是标题。菜单项标题可能会被本地化,使用标题存储数据值很容易被破坏。