2010-04-19 60 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是非常難以使用的,但去年已經有所改進,甚至最終還是得到了改進。祝你好運。