且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Cocoa NSOutlineView和拖放

更新时间:2022-10-15 23:38:29

您没有回应NSOutlineView的拖曳验证讯息。



您的原始代码已实施 tableView:validateDrop:proposedRow:proposedChildIndex:。正如我在这个问题上指出的,这是错误的,当你的表视图是一个大纲视图; NSOutlineView不会发送表视图拖动验证消息,只有大纲视图拖动验证消息。



您已经更改了您的拖拽验证方法像这样:


   - (NSDragOperation)outlineView:(NSOutlineView *)view 
validateDrop :(id< NSDraggingInfo>)info
proposedRow:(int)row
proposedChildIndex:(NSInteger)index




但实际上没有发送这样的消息。



请记住,NSOutlineView很少处理行索引,可以随着父行展开和折叠而更改。因此,正确的验证方法是:

blockquote>
   - (NSDragOperation)outlineView:(NSOutlineView *)view 
validateDrop:(id< NSDraggingInfo>)info
id)item
proposedChildIndex:(NSInteger)index


请注意选择器的第三个组件的名称,以及与其一起使用的参数的类型和名称。



应用此更改后,您的数据源验证丢弃。


I recently started another thread without an account, so I'm reposting the question here with an account so I can edit current links to the program so other users can follow this. I have also updated the code below. Here is my original question:

I read the other post here on Outlineviews and DND, but I can't get my program to work. At the bottom of this post is a link to a zip of my project. Its very basic with only an outlineview and button. I want it to receive text files being dropped on it, but something is wrong with my code or connections. I tried following Apple's example code of their NSOutline Drag and Drop, but I'm missing something. 1 difference is my program is a document based program and their example isn't. I set the File's Owner to receive delegate actions, since that's where my code to handle drag and drop is, as well as a button action. Its probably a simple mistake, so could someone please look at it and tell me what I'm doing wrong? Here is a link to the file: http://dl.dropbox.com/u/7195844/OutlineDragDrop1.zip

You're not responding to NSOutlineView's drag-validation message.

Your original code implemented tableView:validateDrop:proposedRow:proposedChildIndex:. As I pointed out on that question, that's wrong when your table view is an outline view; NSOutlineView will not send a table-view drag-validation message, only an outline-view drag validation message.

You've since changed your drag-validation method to be declared like so:

- (NSDragOperation)outlineView:(NSOutlineView*)view
                validateDrop:(id <NSDraggingInfo>)info
                 proposedRow:(int)row
          proposedChildIndex:(NSInteger)index

But nothing actually sends such a message.

Remember that NSOutlineView rarely deals with row indexes, since those can change as parent rows are expanded and collapsed. It deals instead with "items", which are generally model objects.

Therefore, the correct validation method is:

- (NSDragOperation)outlineView:(NSOutlineView*)view
                validateDrop:(id <NSDraggingInfo>)info
                proposedItem:(id)item
          proposedChildIndex:(NSInteger)index

Notice the name of the third component of the selector, and the type and name of the argument that goes with it.

With this change applied, your data source validates drops.