1

在我的一个项目中使用它之前,我创建了一个小型测试项目来使用 NSOutlineView。我使用内容绑定到数组的 NSTreeController 成功地显示了带有子项的项目列表。

我的 NSTreeConroller 上的绑定

现在,当我创建这个对象时,我花了很长时间才意识到我的数组内容只有在我的 init 方法中创建它们时才会显示: - (id)init { self = [super init];

if (self) {
    results = [NSMutableArray new];

    NSMutableArray *collection = [[NSMutableArray alloc] init];

        // Insert code here to initialize your application
    NSMutableDictionary *aDict = [[NSMutableDictionary alloc] init];
    [aDict setValue:@"Activities" forKey:@"name"];

    NSMutableArray *anArray = [NSMutableArray new];

    for (int i; i<=3 ; i++) {
        NSMutableDictionary *dict = [NSMutableDictionary new];
        [dict setValue:[NSString stringWithFormat:@"Activity %d", i] forKeyPath:@"name"];
        [anArray addObject:dict];
    }

    results = collection;

}

return self;
}

如果我将相同的代码放在 applicationDidFinishLaunching 中,它就不会显示这些项目。

尝试将项目添加到视图时,我现在面临同样的问题。我对使用 NSTreeController 的理解是,它处理的内容类似于 NSArrayController 对 NSTableView 所做的事情(OutlineView 是一个子类和全部)。但是,每当我使用符合 KV 的方法将项目添加到数组时,这些项目都不会显示在我的视图中。

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
NSMutableDictionary *cDict = [[NSMutableDictionary alloc] init];
[cDict setValue:@"Calls" forKey:@"name"];
[results addObject:cDict];
[outlineView reloadData];
}

添加对象后,我还尝试在大纲视图上调用 reloadData,但这似乎没有被调用。我错过了什么?

这是我的项目的链接:https ://dl.dropboxusercontent.com/u/5057512/Outline.zip

4

1 回答 1

1

找到这个答案 后:更改树中的节点后将重新排列对象发送到 NSTreeController 的正确方法?

事实证明,NSTreeControllerperformSelector:@selector(rearrangeObjects) withObject:afterDelay: 在添加对象后做出反应并调用它,让新对象出现。

于 2014-05-03T17:22:02.163 回答