2013-08-07 83 views
2

我試圖在我的NSOutlineView中實現拖放操作,但沒有找到我發現的示例代碼,教程或其他SO問題似乎適用於我的情況。我有一個NSOutlineView,其內容綁定到一個NSTreeController。樹控制器的內容數組綁定到具有相同類型的childern對象的自定義對象的NSMutableArray。在大綱視圖中,我可以在層次結構中的任何級別添加和刪除對象。到現在爲止還挺好。如何使用綁定實現在NSOutlineView中的拖放操作?

實現拖放我創建和NSObject子類將作爲大綱視圖的dataSource。我已經實現了一些基於示例代碼和Stack Overflow上發佈的帖子的方法。我可以啓動拖動,但是當我執行拖放時,outlineView:acceptDrop:item:childIndex:被調用,但除了childIndex:之外的所有值均爲零。 childIndex的值告訴我數組中的放置位置的索引,但不包括我在層次結構內的哪個節點。

我假設在outlineView:acceptDrop:...中傳遞的所有其他值都是零,因爲我沒有完全實現dataSource,我只用它來控制拖放操作。當我開始拖動時,是否需要設置更多的紙板信息?如何找出發生丟失時的節點?爲什麼outlineView中的所有值:acceptDrop:... nil?

這裏是大綱視圖數據源的實現: \ @implementation TNLDragController

- (void)awakeFromNib { 
    [self.titlesOutlineView registerForDraggedTypes:[NSArray arrayWithObject:@"Event"]]; 
} 

- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard { 
    NSLog(@"starting a drag"); 

    NSString *pasteBoardType = @"Event"; 
    [pboard declareTypes:[NSArray arrayWithObject:pasteBoardType] owner:self]; 

     return YES; 
} 

- (NSDragOperation)outlineView:(NSOutlineView *)outlineView 
        validateDrop:(id <NSDraggingInfo>)info 
        proposedItem:(id)item 
      proposedChildIndex:(NSInteger)index { 
    NSLog(@"validating a drag operation"); 
    return NSDragOperationGeneric; 
} 

- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index { 
    NSLog(@"accepting drag operation"); 
    //todo: move the object in the data model; 
    NSIndexPath *path = [self.treeController selectionIndexPath]; // these three values are nil too. 
    NSArray *objects = [self.treeController selectedObjects]; 
    NSArray *nodes = [self.treeController selectedNodes]; 
    return YES; 
} 

// This method gets called by the framework but the values from bindings are used instead 
- (id)outlineView:(NSOutlineView *)outlineView objectValueForTableColumn:(NSTableColumn *)tableColumn byItem:(id)item { 
    return NULL; 
} 

/* 
The following are implemented as stubs because they are required when 
implementing an NSOutlineViewDataSource. Because we use bindings on the 
table column these methods are never called. The NSLog statements have been 
included to prove that these methods are not called. 
*/ 
- (NSInteger)outlineView:(NSOutlineView *)outlineView numberOfChildrenOfItem:(id)item { 
    NSLog(@"numberOfChildrenOfItem"); 
    return 1; 
} 

- (BOOL)outlineView:(NSOutlineView *)outlineView isItemExpandable:(id)item { 
    NSLog(@"isItemExpandable"); 
    return YES; 
} 

- (id)outlineView:(NSOutlineView *)outlineView child:(NSInteger)index ofItem:(id)item { 
    NSLog(@"child of Item"); 
    return NULL; 
} 
@end 
+0

我還沒有嘗試過使用綁定併爲大綱視圖實現DnD,但對於表視圖,您需要繼承數組控制器並添加您的DnD方法。如果你繼承了'NSTreeController',也許同樣的策略可以用於大綱視圖。我的猜測是,儘管在已創建的類中實現數據源方法會更容易。 – Monolo

回答

0

我在這個問題中所述,實際上工作得很好執行,但努力的時候我犯了一個新手的錯誤確定它是否工作。我在- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id <NSDraggingInfo>)info item:(id)item childIndex:(NSInteger)index中設置了一個斷點,以檢查傳入該方法的值。我正在使用ARC,並且這些值從未在方法中引用,因此ARC從未保留它們,使它們無法用於調試器!