Re: NSTableView Drag-Drop re-ordering...
Re: NSTableView Drag-Drop re-ordering...
- Subject: Re: NSTableView Drag-Drop re-ordering...
- From: Michael Robinson <email@hidden>
- Date: Wed, 26 Nov 2008 23:04:08 +1300
(I really did intend for this to reach the list, but I hurriedly pressed
the "reply to sender" hotkey)
Hi Jean-Nicolas
I too had a horrible time implementing this.
Below I've pasted the code that handles dnd re-ordering in my table.
Basically it just makes a copy of the dragged row, deletes it, then
rebuilds the array, inserting the dragged row at its new index.
As I'm still new to ObjC, so I can't really go into details on how it
works - but if you're like me then all you need is a working example!
#define MyPrivateTableViewDataType @"NSMutableDictionary"
- (void)awakeFromNib {
[table registerForDraggedTypes:[NSArray
arrayWithObject:MyPrivateTableViewDataType]];
}
//I'm not actually sure if this method is required... Like I said, I'm
new to ObjC!
- (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet
*)rowIndexes toPasteboard:(NSPasteboard*)pboard
{
// Copy the row numbers to the pasteboard.
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:rowIndexes];
[pboard declareTypes:[NSArray
arrayWithObject:MyPrivateTableViewDataType] owner:controller];
[pboard setData:data forType:MyPrivateTableViewDataType];
return YES;
}
- (NSDragOperation)tableView:(NSTableView*)tv validateDrop:(id
<NSDraggingInfo>)info proposedRow:(int)row
proposedDropOperation:(NSTableViewDropOperation)op
{
// Add code here to validate the drop
NSLog(@"validate Drop");
return NSDragOperationEvery;
}
- (BOOL)tableView:(NSTableView *)aTableView acceptDrop:(id
<NSDraggingInfo>)info row:(int)to
dropOperation:(NSTableViewDropOperation)operation
{
//this is the code that handles dnd ordering - my table doesn't need
to accept drops from outside! Hooray!
NSPasteboard* pboard = [info draggingPasteboard];
NSData* rowData = [pboard dataForType:MyPrivateTableViewDataType];
NSIndexSet* rowIndexes = [NSKeyedUnarchiver
unarchiveObjectWithData:rowData];
int from = [rowIndexes firstIndex];
NSMutableDictionary *traveller = [[controller arrangedObjects]
objectAtIndex:from];
[traveller retain];
int length = [[controller arrangedObjects] count];
NSMutableArray *replacement = [NSMutableArray new];
int i;
for (i = 0; i <= length; i++){
if(i == to){
if(from > to){
[controller insertObject:traveller atArrangedObjectIndex:to];
[controller removeObjectAtArrangedObjectIndex:from+1];
}
else{
[controller insertObject:traveller atArrangedObjectIndex:to];
[controller removeObjectAtArrangedObjectIndex:from];
}
}
}
}
Jean-Nicolas Jolivet wrote:
<div class="moz-text-flowed" style="font-family: -moz-fixed">I'm
pretty sure this has been covered before, however I can't find any
good examples on how to implement it??
Right now my NSTableView is hooked to a subclass of NSArrayController,
and it supports files getting dropped on it.. (basically the
NSTableView displays files dropped from the finder)... that part works
well, however, now I would like to implement drag/drop re-ordering of
rows...
Any good examples on how to do that?
Jean-Nicolas Jolivet
email@hidden
http://www.silverscripting.com
</div>
_______________________________________________
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