3

I have a Mac OS X application that uses an NSOutlineView with two columns: key and value, where you can edit the value column. I either have a NSString or a NSDictionary in a row. The code for the value of the cells is like this:

- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item {

    if ([[[tableColumn headerCell] stringValue] isEqualToString:@"Key"]) {
        id parentItem = [outlineView parentForItem:item] ? [outlineView parentForItem:item] : root;

        return [[parentItem allKeysForObject:item] objectAtIndex:0];
    } else {
        if ([item isKindOfClass:[NSString class]]) {
            return item;
        } else if ([item isKindOfClass:[NSDictionary class]]) {
            return @"";
        } else {
            return nil;
        }
    }
}

It's working as it should, except for when to value fields has the same string value. It always just takes the first element with that value to show as the key, so the same key value will appear for all value values that are the same. Anybody know how to fix this problem?

4

1 回答 1

2

看起来您正在显示一个字典树,其对象是字符串或字典。

第一个问题是每个项目对象必须唯一标识一行。键和值都没有这个属性。(如果这是一个平面表视图,则键将是,但这是一个大纲视图,并且两个字典 - 一个是另一个的后代,兄弟或表亲 - 可以具有相同的键。)相反,您应该创建一个模型对象对于每个键值对。

其次,字典值行应该是组项。您可以为此实现一个委托方法。

于 2009-11-23T00:33:04.190 回答