gpt4 book ai didi

objective-c - 尽管我已经实现了委托(delegate)方法,但未为 NSOutlineView 启用拖放

转载 作者:太空狗 更新时间:2023-10-30 04:00:25 24 4
gpt4 key购买 nike

我无法为 NSOutlineView 启用拖放功能。我已经实现了 NSOutlineView Delegate 的相关方法。

但是好像我点击一个item的时候,我什至不能拖动它(我没有看到动画)。

- (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.
}

谢谢

更新:

我正在为 OSX >= 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;
}

最佳答案

您实现的方法只是针对拖动的目的地。您仍然需要实现 dragging source methods .无论出于何种原因,Apple 的 NSOutlineViewDataSource Protocol文档缺少这些方法,但您有两个选择:

  1. 如果您构建的是 10.7+,请使用 Xcode 的“快速打开”命令查看 NSOutlineView.h 并找到相关方法。另请查看 DragNDropOutlineView示例应用。

  2. 如果您支持以前的操作系统,则使用 NSTableView 的委托(delegate)方法。参见 NSTableViewDataSource Protocol Reference .请记住,NSOutlineView 是 NSTableView 的子类,可以使用 TableView 方法。

至少您可能想要实现 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: 所做的粘贴板类型.您可以在 View Controller 的某处执行此操作,可能是在 awakeFromNib 中。

[outlineView registerForDraggedTypes:[NSArray arrayWithObject:@"myPasteBoardType"]];

要移动项目(及其所有子项),请修改 outlineView:acceptDrop:item:childIndex: 中的模型。然后发送reloadData到outlineView。

关于objective-c - 尽管我已经实现了委托(delegate)方法,但未为 NSOutlineView 启用拖放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9978472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com