nStableViewを使用してnspopupbuttoncellを使用します
-
28-09-2019 - |
質問
こんにちは私は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
解決
私はそれを理解したと思います。メニュー項目がクリックされた後、セルのテキストを更新するために、ポップアップセルのメソッド「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
テーブルビューでは、データモデルに加えた変更でそれ自体を更新するように強制します。 nspopupbuttoncellは、アイテムインデックスをオブジェクト値として使用するため、WillDisplayCellデリゲートメソッドのコードなしでは、物事の一部が正しく機能するはずです。
余談ですが、使用することをお勧めします representedObject
タイトルではなくアイテム部分を伝えるメニュー項目のプロパティ。メニュー項目のタイトルをローカライズする可能性があり、タイトルを使用してデータ値を保存することは、破損しやすくなります。