Мне сложно скрестить достаточное количество фрагментов знаний для реализации NSOutlineView со статической, неизменяемой структурой, определенной в NSArray. This link было здорово, но это не помогает мне схватить подменю. Я думаю, что они просто вложенные NSArrays, но у меня нет четкой идеи.Реализация статического NSOutlineView
Допустим, мы имеем 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 показывает массив, начиная с «(», следовательно, второй пункт. Расширение он вызывает сбой из-за происходит ' за пределы «Я попытался с помощью parentForItem:.., но не мог понять, что сравнивать результат
Что я упускаю
Вложенные списки свойств - это верный путь к боли. Вместо этого вы должны создавать и работать с объектами модели. http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ModelObjects/ Кроме того, не смотрите заголовок заголовка столбца, который должен меняться в зависимости от текущего языка (после локализации вашего приложение); вместо этого используйте идентификатор столбца. –