我无法为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.
}
感谢
更新:
我正在实施forOSX&gt; = 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;
}
答案 0 :(得分:5)
您实施的方法仅适用于拖动的目的地。您仍然需要实施dragging source methods。无论出于何种原因,Apple的NSOutlineViewDataSource Protocol文档都缺少这些方法,但您有两种选择:
如果您正在构建10.7+,请使用Xcode的Open Quickly命令查看NSOutlineView.h并找到相关方法。另请查看DragNDropOutlineView示例应用。
如果您支持以前的操作系统,那么请使用NSTableView的委托方法。见NSTableViewDataSource Protocol Reference。请记住,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;
更新
如果可以拖动项目但不会丢弃任何内容,则很可能不会调用outlineView:validateDrop:proposedItem:proposedChildIndex:
。这意味着您尚未注册使用registerForDraggedTypes:
执行的粘贴板类型。您可以在视图控制器中的某个位置执行此操作,可能在awakeFromNib
。
[outlineView registerForDraggedTypes:[NSArray arrayWithObject:@"myPasteBoardType"]];
要移动项目(及其所有子项),请在outlineView:acceptDrop:item:childIndex:
中修改模型。然后将reloadData
发送到outlineView。
答案 1 :(得分:3)
要使大纲视图成为拖动源,您必须实现:
- (BOOL)outlineView:(NSOutlineView *)outlineView writeItems:(NSArray *)items toPasteboard:(NSPasteboard *)pasteboard;
这应该解决你所描述的问题,但除此之外你还有很多工作要做。