私は、4 つの列を持つ NSTableView を持つこのプログラムを作成しています。そのうちの 2 つはチェックボックスで構成されています。私は今、1つだけを機能させようとしていますが、行き詰まっています。
まず、関連するコードは次のとおりです。
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView {
NSString *filePathThree = [[NSBundle mainBundle] pathForResource:@"mydictionary" ofType:@"plist"];
NSData *myDataThree = [[NSData alloc]initWithContentsOfFile:filePathThree];
self.flozzCodeAndName = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:myDataThree
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:NULL
errorDescription:NULL];
return [[flozzCodeAndName objectForKey:@"name"] count];
}
- (void)tableView:(NSTableView *)tableView
setObjectValue:(id)anObject forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)rowIndex
{
NSButtonCell *cell;
cell = [[NSButtonCell alloc] init];
[cell setButtonType:NSSwitchButton];
[cell setTitle:@""];
[cell setTag:rowIndex];
NSLog(@"%d", [cell tag]);
[cell setCellAttribute:NSCellEditable to:3];
[cell setImagePosition:NSImageOnly];
[cell setState:NSOnState];
NSLog(@"%d", [cell state]);
[havzColumn setDataCell:cell];
[myTableVeew reloadData];
[cell release];
}
- (id)tableView:(NSTableView *)aTableView
objectValueForTableColumn:(NSTableColumn *)aTableColumn row:(NSInteger)rowIndex {
NSString *filePathThree = [[NSBundle mainBundle] pathForResource:@"mydictionary" ofType:@"plist"];
NSData *myDataThree = [[NSData alloc]initWithContentsOfFile:filePathThree];
self.flozzCodeAndName = (NSMutableDictionary *)[NSPropertyListSerialization
propertyListFromData:myDataThree
mutabilityOption:NSPropertyListMutableContainersAndLeaves
format:NULL
errorDescription:NULL];
NSArray *myArray = [flozzCodeAndName objectForKey:[aTableColumn identifier]];
NSString *myStringValue = [myArray objectAtIndex:rowIndex];
return myStringValue;
}
ご覧のとおり、このテーブルではバインディングではなくデータ ソース メソッドを使用しています。私が読んだ Cocoa の本では、タグ付きのチェックボックスがいくつか作成されていましたが、それらは配列になっていると思うので、それは最善の方法ではないかもしれません。
とにかく、これを実行すると、デバッガーはボタンの状態 (NSOnState のため、すべて 1) と共にタグ (行に相当) を表示します。私の問題は、ボックスの状態に応じてチェックボックスをオンまたはオフにすることができないことです。私はこの質問を読みました:テーブル列のチェックボックスはクリックを登録しません
そして、NSTableView データソース参照。リンクされた質問の Nozzi 氏によると、ボックスの状態を含む配列が必要であるように思われるので[cell state]
、NSNumber に設定して NSMutableArray に入れてみました。私はそれを FUBAR しましたが、それが正しいとは思いません。このテーブルには、4 つの列すべてに対して 454 行あります (配列が 0 から始まるため、タグは 453 になります)。
また、tableview:setObjectValue: にあるセル定義のものを「awakeFromNib」に入れる必要があるかどうかも疑問です。IB にチェックボックス ボタン セルを配置しましたが、以前に問題があったため、プログラムでも行うことにしました。これらすべての間、私は[myTableVeew reloadData]
setObjectValue メソッドに を持っていましたが、今でも持っています。
他の情報が必要な場合は、私が入手できます。