使用绑定填充NSOutlineView - KVO添加项目

时间:2014-05-03 06:32:51

标签: cocoa cocoa-bindings nsoutlineview nstreecontroller

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

Bindings on my 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];
}

我还尝试在添加对象后在outlineview上调用reloadData,但似乎没有调用。我错过了什么?

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

1 个答案:

答案 0 :(得分:1)

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

事实证明,NSTreeController对performSelector:@selector(rearrangeObjects) withObject:afterDelay:作出反应 并在添加对象后调用它可以显示新对象。