2010-04-19 66 views
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:。但想不出什么结果来比较

我缺少什么

+0

嵌套属性列表是一个可靠的途径。您通常应该创建并使用模型对象。 http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ModelObjects/另外,不要看列标题的标题,它应该根据当前的语言而改变(一旦你已经本地化应用程序);改用列标识符。 – 2010-04-19 17:46:31

回答

0

包含的是链接背后的例子显示了一个NSDictionary照顾子阵的?所以我认为你的ovStructure不应该是一个数组,而应该是一个字典,但是更重要的是,我认为你应该看看NSTreeController。不幸的是,NSTreeController是非常难以使用的,但去年已经有所改进,甚至最终还是得到了改进。祝你好运。