Separate names with a comma.
Discussion in 'Mac Programming' started by fernandovalente, Aug 21, 2010.
I would like to do drag and drop between 2 NSTableViews in the same app. How may I do it?
If I had two multi-column tables of more-or-less the same type, such as two different documents, I would consider two possibilities.
First, if the columns will always be the same, you can match the cell values by identifier. Otherwise (if the user can add or remove columns or edit their headers), I would match the cell values by the header title, only putting in values for header titles that match. Either of these techniques would be appropriate for other types of transfers.
The drag source table must write a type to the pasteboard that the receiving table recognizes (included in the -registerForDraggedTypes: method argument). The source table should fill a dictionary with cell values keyed to either their column identifier or header title, filling an array with these dictionaries for each row dragged. It will write this to the pasteboard using -setPropertyList:forType:, which the destination table will read from the pasteboard with -propertyListForType: and add or insert rows, using the keys to match cell values to the appropriate columns.
You should select a type name for this data that will most likely not be recognized by any other app that might see the drag pass its window ("Fernando'sApp'sTableDataSpecialType" or something like that), though it is probably good practice to write other types to the pasteboard, such as text, so that the user might be able to drop the data onto some other app.