我正在试图弄清楚如何在NSOutlineView中实现拖放到自身。
例如,我希望用户能够重新排序并嵌套和重新嵌套NSOutlineView中的项目,但我不需要能够从任何其他来源(如文件或其他视图)拖动项目)。
我能找到的所有示例都涉及将项目拖动到NSOutlineView中,而不仅仅是在其内部并且看起来过于复杂。我认为这是可能的。
有人可以指出一些只处理这种情况的文档吗?也许我只是错过了显而易见的事情。
答案 0 :(得分: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
}
最后一种方法需要填写大量代码,对于动画运动,samir评论中提到的Apple complex table view demo code非常有用,如果解密起来有点复杂。
答案 1 :(得分:8)