【问题标题】:Populating NSOutlineView with bindings - KVO adding of items使用绑定填充 NSOutlineView - KVO 添加项目
【发布时间】:2014-05-03 06:32:50
【问题描述】:

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

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

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

【问题讨论】:

    标签: cocoa cocoa-bindings nsoutlineview nstreecontroller


    【解决方案1】:

    找到这个答案后: Correct way to get rearrangeObjects sent to an NSTreeController after changes to nodes in the tree?

    原来 NSTreeController 对performSelector:@selector(rearrangeObjects) withObject:afterDelay: 做出反应 并在添加对象后调用它会让新对象出现。

    【讨论】:

    • 如果你不强制立即执行rearrangeObjects,我相信它会延迟到下一次runloop迭代,比如fetch
    猜你喜欢
    • 2016-05-18
    • 1970-01-01
    • 2011-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-19
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多