【问题标题】:Setting up a NSTreeCcontroller for a NSOutlineView using CoreData使用 CoreData 为 NSOutlineView 设置 NSTreeCcontroller
【发布时间】:2013-02-03 21:57:37
【问题描述】:

我的模型如下所示:

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

- (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];
}

这会产生如下所示的大纲视图:

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

如您所见,当子项应该只出现在根项下时,我得到了重复的条目。

我做错了什么?

示例项目位于sample project link。

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 那么,您是说我的 NSTreeController 在属性检查器中需要为对象控制器部分的 Fetch Predicate 设置一个值? Fetch Predicate 会是什么样子?
    猜你喜欢
    • 2012-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-18
    相关资源
    最近更新 更多