6

こんにちは私は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

これは、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
4

2 に答える 2

3

私はそれを理解したと思います。ポップアップセルのメソッド「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];
    }
}
于 2010-08-15T02:14:16.367 に答える
2

に値を設定した後に行う必要があるのtableView:setObjectValue:forTableColumn:row:は、テーブルビューを呼び出しreloadDataて、データモデルに加えた変更でテーブルビューを強制的に更新することだけだと思います。NSPopUpButtonCellは、オブジェクトインデックスとしてアイテムインデックスを使用するため、willDisplayCellデリゲートメソッドにそのコードがなくても、一部が正しく機能するはずです。

余談representedObjectですが、メニュー項目のプロパティを使用して、タイトルではなく項目の部分を示すことをお勧めします。メニュー項目のタイトルはローカライズされる可能性があり、タイトルを使用してデータ値を格納すると、破損する可能性が非常に高くなります。

于 2010-08-15T05:04:53.613 に答える