NSArray で定義された静的で決して変化しない構造を持つ NSOutlineView を実装するのに十分な知識の断片をかき集めるのに苦労しています。このリンクはすばらしいものでしたが、サブメニューを把握するのに役立っていません。私はそれらがネストされた NSArrays だと思っていますが、明確な考えはありません。
次のように定義された NSArray 内に NSArray があるとします。
NSArray *subarray = [[NSArray alloc] initWithObjects:@"2.1", @"2.2", @"2.3", @"2.4", @"2.5", nil];
NSArray *ovStructure = [[NSArray alloc] initWithObjects:@"1", subarray, @"3", nil];
テキストは、outlineView:objectValueForTableColumn:byItem: で定義されます。
- (id)outlineView:(NSOutlineView *)ov objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)ovItem
{
if ([[[tableColumn headerCell] stringValue] compare:@"Key"] == NSOrderedSame)
{
// Return the key for this item. First, get the parent array or dictionary.
// If the parent is nil, then that must be root, so we'll get the root
// dictionary.
id parentObject = [ov parentForItem:ovItem] ? [ov parentForItem:ovItem] : ovStructure;
if ([parentObject isKindOfClass:[NSArray class]])
{
// Arrays don't have keys (usually), so we have to use a name
// based on the index of the object.
NSLog([NSString stringWithFormat:@"%@", ovItem]);
//return [NSString stringWithFormat:@"Item %d", [parentObject indexOfObject:ovItem]];
return (NSString *) [ovStructure objectAtIndex:[ovStructure indexOfObject:ovItem]];
}
}
else
{
// Return the value for the key. If this is a string, just return that.
if ([ovItem isKindOfClass:[NSString class]])
{
return ovItem;
}
else if ([ovItem isKindOfClass:[NSDictionary class]])
{
return [NSString stringWithFormat:@"%d items", [ovItem count]];
}
else if ([ovItem isKindOfClass:[NSArray class]])
{
return [NSString stringWithFormat:@"%d items", [ovItem count]];
}
}
return nil;
}
結果は '1'、'(' (展開可能)、'3' です。NSLog は '(' で始まる配列を示しているため、2 番目の項目です。これを展開すると、'境界を超えている' ため、クラッシュが発生します。 parentForItem: しかし、結果を比較する対象がわかりませんでした。
私は何が欠けていますか?