1

我已经在我的应用程序中实现了拖放功能。所有功能都很好,但是当我们将图像拖入时NSTableView

- (BOOL)performDragOperation:(id < NSDraggingInfo >)sender

方法没有被调用。

谁能告诉我这个方法没有被调用的原因?

即使我也实现了这个......

- (BOOL)prepareForDragOperation:(id < NSDraggingInfo >)sender
4

1 回答 1

6

We need a lot more information.

"...but when we drag a image in to NSTableView"

What do you mean by "image", and where (what application) are you dragging this image from? For example, do you mean an image file (Picture.png) from the Finder that you are dragging to the table view in your application? Or from your own application are you dragging some image from one place to your table view?

Is this your own custom subclass of NSTableView? Because that's the only place that you will see -performDragOperation: or -prepareForDragOperation: being called. By default, NSTableView overrides those primitive NSDraggingDestination methods to implement its own tableview-oriented type of methods like Bavarious mentioned (-tableView:validateDrop:proposedRow:proposedDropOperation:, -tableView:acceptDrop:row:dropOperation:, etc.). If you are talking about the fact that these methods aren't being called in an NSTableView subclass, then remember what the documentation states for -prepareForDragOperation::

This method is invoked only if the most recent draggingEntered: or draggingUpdated: message returned an acceptable drag-operation value

So, first, you need to make sure you've registered for the drag types you want, then you need to implement -draggingEntered.

If, on the other hand, you aren't talking about an NSTableView subclass, but an external controller class, then, yes, those performDragOperation: and prepareForDragOperation: aren't called for it. In other words, if you have a controller class, say, MDAppController, it's set to be the delegate and datasource of an NSTableView, the -performDragOperation: and prepareForDragOperation: of MDAppController won't be called. Those methods are meant for NSView-based classes. For that reason, NSTableView has the following method defined in the NSTableViewDataSource protocol: tableView:validateDrop:proposedRow:proposedDropOperation:. If you implement that in your controller class, it should be called, provided you've set the tableView up properly and it's been registered for the types of data you want.

于 2011-05-19T09:08:22.573 回答