使用Core Data为NSOutlineView设置NSTreeController

时间:2013-02-03 21:57:37

标签: cocoa core-data cocoa-bindings nsoutlineview nstreecontroller

我的模型看起来像这样:

model

在我的测试项目中,我有以下两种方法:

- (void) addChildWithName:(NSString*)name toParent:(Item*)parent
{
    static NSUInteger count = 1;

    Item*   childItem;


    childItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:[self managedObjectContext]];
    [childItem setName:[NSString stringWithFormat:@"%@ %lu", name, count]];
    [childItem setParent:parent];

    count++;
}



- (void)windowControllerDidLoadNib:(NSWindowController *)aController
{
    [super windowControllerDidLoadNib:aController];
    // Add any code here that needs to be executed once the windowController has loaded the document's window.


    Item*   rootItem;

    rootItem = [NSEntityDescription insertNewObjectForEntityForName:@"Item" inManagedObjectContext:[self managedObjectContext]];
    [rootItem setName:@"rootItem"];

    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
    [self addChildWithName:@"rootChild" toParent:rootItem];
}

这会产生一个大纲视图,如下所示:

result

对于xib中的树控制器对象,我将Children Key Path设置为'children'。托管对象上下文(moc)绑定到文件所有者moc。我的大纲视图的表列的值绑定到NSTreeController的arrangeObjects,其模型键路径为'name'。

正如您所看到的,当子项目只应出现在根项目下时,我会收到重复的条目。

我做错了什么?

示例项目位于sample project link

谢谢。

1 个答案:

答案 0 :(得分:2)

当使用Item parent属性为零时,您的数组控制器需要“获取谓词”才能选择parent == nil个对象。

相关问题