// A method to register the view for dragged items from itself.
// Call it during initialization
-(void) enableDragNDrop
{
[self.outlineView registerForDraggedTypes: [NSArray arrayWithObject: @"DragID"]];
}
- (id <NSPasteboardWriting>)outlineView:(NSOutlineView *)outlineView pasteboardWriterForItem:(id)item
{
// No dragging if <some condition isn't met>
if ( dontDrag ) {
return nil;
}
// With all the blocking conditions out of the way,
// return a pasteboard writer.
// Implement a uniqueStringRepresentation method (or similar)
// in the classes that you use for your items.
// If you have other ways to get a unique string representation
// of your item, you can just use that.
NSString *stringRep = [item uniqueStringRepresentation];
NSPasteboardItem *pboardItem = [[NSPasteboardItem alloc] init];
[pboardItem setString:stringRep forType: @"DragID"];
return pboardItem;
}
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
{
BOOL dragOK = // Figure out if your dragged item(s) can be dropped
// on the proposed item at the index given
if ( dragOK ) {
return NSDragOperationMove;
}
else {
return NSDragOperationNone;
}
}
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)index
{
// Move the dragged item(s) to their new position in the data model
// and reload the view or move the rows in the view.
// This is of course quite dependent on your implementation
}
2 回答
我创建了一个small sample project,它有一个
NSOutlineView
,您可以在其中添加和删除项目 .今天我基于@Monolo的答案(See changes)实现了拖放支持 .
我发现这个文档有点不清楚,除此之外,还有新的方法在Lion中添加 .
假设您需要10.7或更高版本,那么这可能是您实现的框架:
在您的数据源/委托类中,实现:
对于最后一种方法,需要填写大量代码,对于动画运动,在samir的评论中提到的Apple的complex table view demo code非常有用,如果解密起来有些复杂的话 .