我无法为NSOutlineView启用拖放功能.我已经实现了NSOutlineView Delegate的相关方法.
但似乎当我点击一个项目时,我甚至无法拖动它(我看不到动画).
- (BOOL)outlineView:(NSOutlineView *)outlineView acceptDrop:(id < NSDraggingInfo >)info item:(id)item childIndex:(NSInteger)index
{
return YES;
}
- (NSDragOperation)outlineView:(NSOutlineView *)outlineView validateDrop:(id < NSDraggingInfo >)info proposedItem:(id)item proposedChildIndex:(NSInteger)index
{
return NSDragOperationMove; //not sure about this one.
}
Run Code Online (Sandbox Code Playgroud)
谢谢
更新:
我正在实现forOSX> = 10.5
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pboard
{
NSString *pasteBoardType = [self pasteboardTypeForTableView:outlineView];
[pboard declareTypes:[NSArray arrayWithObject:pasteBoardType] owner:self];
NSData *rowData = [NSKeyedArchiver archivedDataWithRootObject:items];
[pboard setData:rowData forType:pasteBoardType];
return YES;
}
Run Code Online (Sandbox Code Playgroud)
您实现的方法仅适用于拖动的目标.您仍然需要实现拖动源方法.无论出于何种原因Apple的NSOutlineViewDataSource Protocol文档缺少这些方法,但您有两个选择:
如果要构建10.7+,请使用Xcode的Open Quickly命令查看NSOutlineView.h并找到相关方法.另请查看DragNDropOutlineView示例应用程序.
如果您支持以前的操作系统,那么请使用NSTableView的委托方法.请参阅NSTableViewDataSource协议参考.请记住,NSOutlineView是NSTableView的子类,可以使用表视图方法.
至少你可能想要实现 outlineView:writeItems:toPasteboard:
/* Dragging Source Support - Optional for single-image dragging. This method is called after
it has been determined that a drag should begin, but before the drag has been started. To
refuse the drag, return NO. To start a drag, return YES and place the drag data onto the
pasteboard (data, owner, etc...). The drag image and other drag related information will
be set up and provided by the outline view once this call returns with YES. The items array
is the list of items that will be participating in the drag.
*/
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard;
Run Code Online (Sandbox Code Playgroud)
更新:
如果项目可以被拖动但不会掉落任何东西,那么很可能 outlineView:validateDrop:proposedItem:proposedChildIndex:不会被调用.这意味着您尚未注册使用的粘贴板类型registerForDraggedTypes:.你可以在视图控制器中的某个地方执行此操作,可能在awakeFromNib.
[outlineView registerForDraggedTypes:[NSArray arrayWithObject:@"myPasteBoardType"]];
Run Code Online (Sandbox Code Playgroud)
要移动项目(及其所有子项),请修改模型outlineView:acceptDrop:item:childIndex:.然后发送reloadData到outlineView.