代码之家  ›  专栏  ›  技术社区  ›  spamguy

实现静态nsOutlineView

  •  0
  • spamguy  · 技术社区  · 14 年前

    我很难收集到足够多的知识片段来实现一个静态的、永不改变的NSarray中定义的结构的nsOutlineView。 This link 已经很好了,但它不能帮助我掌握子菜单。我想它们只是嵌套的NSarray,但我不清楚。

    假设我们在一个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:但无法找到将结果与之进行比较的方法。

    我错过了什么?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Elise van Looij    14 年前

    您包含的链接后面的示例显示了一个nsdictionary,它负责处理子数组的内容(如果我正确阅读的话)。所以我认为您的ovstructure不应该是数组,而应该是字典。但是,从根本上说,我认为你真的应该研究一下NStreecontroller。不幸的是,nstreecontroller是出了名的努力工作,但去年有了一些改进,甚至我最后也成功了。祝你好运。