使用CoreData设置NSTreeController以用于NSOutlineView

3
我的模型长这样:

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的arrangedObjects,并具有“name”的模型键路径。
正如您所看到的,当子项应该仅出现在根项下时,我正在获取子项的重复条目。
我做错了什么?
示例项目位于 sample project link
谢谢。
1个回答

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

所以,你是说我的NSTreeController,在属性检查器中,需要为对象控制器部分的Fetch Predicate设置一个值?那个Fetch Predicate会是什么样子? - ericg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接