2

我的模型看起來像這樣:建立一個NSTreeCcontroller的NSOutlineView使用CoreData

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'。託管對象上下文(moc)綁定到文件所有者moc。我的大綱視圖的表列的值綁定到NSTreeController的arrangeObjects,其中模型關鍵路徑爲'name'。

正如您所看到的,當它們只應出現在根項目下時,我將獲取子項目的重複條目。

我在做什麼錯?

該示例項目位於sample project link

謝謝。

回答

2

你的陣列控制器需要一個「獲取謂語」,只選擇Item對象時,parent屬性是nil使用parent == nil

+0

所以,你說我的NSTreeController,在屬性檢查器中,需要爲對象控制器部分中的提取謂詞設置一個值?那個Fetch Predicate會是什麼樣子? – ericg

+1

'parent == nil' –

+0

謝謝。這樣可行。 – ericg