Re: Row dragging ...
Re: Row dragging ...
- Subject: Re: Row dragging ...
- From: J Nozzi <email@hidden>
- Date: Sun, 1 Feb 2004 20:30:30 -0500
Thanks to all who have replied (both privately and to the list) and
given me plenty of examples to work with. My lack of understanding of
Bindings is my biggest problem.
Thanks again!
- J
On Jan 31, 2004, at 5:22 PM, Herr Witten wrote:
Read this:
http://developer.apple.com/documentation/Cocoa/Conceptual/TableView/
Tasks/UsingDragAndDrop.html#//apple_ref/doc/uid/20000726
There are three methods you need to implemen in the data sourcet:
This method when a drag is begun. The table calls this method on the
data source, so if you have an NSArrayController, make it the data
source and define this method with a category or a subclass. You are
supposed to supply the data to the pasteboard, which is passed on
throughout the process. If you have to much data to pass or strange
data, then you can specify that you want to have the destination
object use a callback method to get the data when it is ready. See the
documentation for details
- (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows
toPasteboard:(NSPasteboard *)pboard;
For example:
- (BOOL)tableView:(NSTableView *)tableView writeRows:(NSArray *)rows
toPasteboard:(NSPasteboard *)pboard
{
[_draggingObjects release];
_draggingObjects = [[NSMutableArray alloc] init];
NSEnumerator* rowsEnumerator = [rows objectEnumerator];
while (NSNumber* row = [rowsEnumerator nextObject])
[_draggingObjects addObject: [[self arrangedObjects]
objectAtIndex: [row unsignedIntValue]]];
NSData* data = [NSKeyedArchiver archivedDataWithRootObject:
_draggingObjects];
[pboard declareTypes: [NSArray arrayWithObject: @"draggingData"]
owner: nil];
[pboard setData: data forType: @"draggingData"];
return YES;
}
This method is used to validate a drop and it is sent to the data
source by the destination table. Your data source should check the
conditions of the drop and return the operations that are allowed.
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id
<NSDraggingInfo>)info proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)operation
For example:
- (NSDragOperation)tableView:(NSTableView *)tableView validateDrop:(id
<NSDraggingInfo>)info proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)operation
{
if (_tableLocked)
return NSDragOperationNone;
else
return NSDragOperationMove;
}
This method is sent to the data source of the destination table when
the user release the dragged object. It allows the data source to
incorporate the data appropriately for the table.
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id
<NSDraggingInfo>)info row:(int)row
dropOperation:(NSTableViewDropOperation)operation
For example:
- (BOOL)tableView:(NSTableView *)tableView acceptDrop:(id
<NSDraggingInfo>)info row:(int)row
dropOperation:(NSTableViewDropOperation)operation
{
NSMutableArray* content = [NSMutableArray arrayWithArray: [[self
content] copy]];
NSArray* objects = [NSKeyedUnarchiver unarchiveObjectWithData:
[[info draggingPasteboard] dataForType: @"draggingData"]];
/*go through objects and insert them into the proper places*/
return YES;
}
This code assumes the data source is an NSArrayController.
On 31 Jan 2004, at 4:44 PM, J Nozzi wrote:
I've been searching and searching and unable to find a clear,
concise answer to the following:
How in the world do you make an NSTableView able to respond to row
drags? I want the user to be able to re-order rows (not columns) with
a drag operation.
If anyone has a very simple explanation, I'd be most appreciative.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.