Rog*_*rat 8 cocoa drag-and-drop nsoutlineview
我正在试图弄清楚如何在NSOutlineView中实现拖放到自身.
例如,我希望用户能够重新排序并嵌套和重新嵌套NSOutlineView中的项目,但我不需要能够从任何其他来源(如文件或其他视图)拖动项目.
我可以找到的所有示例都处理将项目拖动到NSOutlineView中,而不仅仅是在其自身内部并且看起来过于复杂.我认为这是可能的.
有人可以指出一些只处理这种情况的文档吗?也许我只是错过了显而易见的事实.
Mon*_*olo 14
我发现这个文档有点不清楚,除此之外,还有新的方法在Lion中添加.
假设您需要10.7或更高版本,那么这可能是您实现的框架:
在您的数据源/委托类中,实现:
// 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
}
Run Code Online (Sandbox Code Playgroud)
对于最后一种方法,需要填写大量代码,对于动画运动,在samir的注释中提到的Apple的复杂表格视图演示代码非常有用,如果解密有些复杂的话.