0
我很難拼湊足夠的知識片段來實現一個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/另外,不要看列標題的標題,它應該根據當前的語言而改變(一旦你已經本地化應用程序);改用列標識符。 – 2010-04-19 17:46:31