我试图弄清楚如何实现从 NSOutlineView 到自身的拖放。
例如,我希望用户能够对 NSOutlineView 中的项目进行重新排序、嵌套和重新嵌套,但我不需要能够从任何其他源(如文件或其他视图)拖动项目。
我能找到的所有示例都涉及将项目拖动到 NSOutlineView 中,而不仅仅是在其内部并且看起来过于复杂。我认为这是可能的。
有人可以指出一些只处理这种情况的文档吗?也许我只是错过了显而易见的事情。
我试图弄清楚如何实现从 NSOutlineView 到自身的拖放。
例如,我希望用户能够对 NSOutlineView 中的项目进行重新排序、嵌套和重新嵌套,但我不需要能够从任何其他源(如文件或其他视图)拖动项目。
我能找到的所有示例都涉及将项目拖动到 NSOutlineView 中,而不仅仅是在其内部并且看起来过于复杂。我认为这是可能的。
有人可以指出一些只处理这种情况的文档吗?也许我只是错过了显而易见的事情。
我发现这方面的文档有些不清楚,除其他外,在 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 的复杂表格视图演示代码非常有用,虽然有点难以破译。