Re: NSTableView - Drag/Drop not working
Re: NSTableView - Drag/Drop not working
- Subject: Re: NSTableView - Drag/Drop not working
- From: Conrad Shultz <email@hidden>
- Date: Sun, 09 Oct 2011 02:22:35 -0700
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On 10/7/11 10:25 PM, GW Rodriguez wrote:
> I have gone through many different threads on this, read the docs
> and went over a few example projects. For some reason I cannot
> get reordering via drag and drop to work. I have even created a
> new project with only a NSTableView with one column and pasted code
> from a working example and it still wont work.
>
> My guess is my error is somewhere around the
> registerForDraggedTypes method because I cannot even start the
> drag. It only start to highlight multiple selections. I dont
> fully understand that method and I should also note that all the
> example projects are document based projects and mine is not.
Code would really help here.
To get to the point where dragging *begins* you need to minimally do
the following:
1) Have a data source (an object that, in 10.6 and newer, conforms to
NSTableViewDataSource) properly set for your table. I repeat: make
sure your data source is assigned to the object in which you are
implementing your dragging methods.
2) In your data source, return YES from
–tableView:writeRowsWithIndexes:toPasteboard:.
If you do this, you will be able to start a drag (but won't be able to
drop it anywhere yet).
Note what I did not mention:
* Whether the app is document based is irrelevant.
* You DON'T need to call -registerForDraggedTypes:. This method is
used to set which pasteboard types the view potentially _accepts_ from
a drag. If you don't register one or more types, the table view won't
let you drop a row that you are dragging, but it will let you begin a
dragging session.
* You DON'T need to implement anything other than a return in
–tableView:writeRowsWithIndexes:toPasteboard:. Of course, if you
don't you won't actually be storing any pasteboard data, so the drag
will be useless - but the drag will still visually begin.
So, check on your implementation of the two steps listed earlier. You
might consider breaking on
–tableView:writeRowsWithIndexes:toPasteboard: to make sure it's
getting called.
After you have nailed that down and can see a drag begin, you will
need to flesh out the full dragging procedure. In particular:
1) DO call -registerForDraggedTypes: and give it an array of UTIs; for
a table view, this may be a single custom UTI you define for internal
use and which you will use in the dragging methods where required.
2) DO fully implement –tableView:writeRowsWithIndexes:toPasteboard:.
For row reordering purposes, it may be as simple as:
- - (BOOL)tableView:(NSTableView *)tableView
writeRowsWithIndexes:(NSIndexSet *)rowIndexes
toPasteboard:(NSPasteboard *)pboard
{
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray
arrayWithObject:MyAwesomeCustomUTIStringThatIDeclaredSomewhere]
owner:self];
[pboard setData:data
forType:MyAwesomeCustomUTIStringThatIDeclaredSomewhere];
return YES;
}
3) DO implement
- -tableView:validateDrop:proposedRow:proposedDropOperation:, most
likely returning NSDragOperationMove for a row reordering operation.
4) DO implement -tableView:acceptDrop:row:dropOperation: to reorder
the rows and/or update the model (if appropriate), typically returning
YES if it's a simple row reordering.
Good luck - feel free to post a link to your sample project if you
can't get it to work.
- --
Conrad Shultz
Synthetiq Solutions
www.synthetiqsolutions.com
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.14 (Darwin)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/
iEYEARECAAYFAk6RZ9sACgkQaOlrz5+0JdVHhgCfcYVJb9z4LPGSYse8lqlL1j9Z
Y84AnjBIrGlea59mWTKqLnnVejOBxXUg
=YAKg
-----END PGP SIGNATURE-----
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden