【发布时间】: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