--- On Wed, 26/11/08, Michael Robinson <[EMAIL PROTECTED]> wrote: > 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.
This approach probably wouldn't work if you allow dragging of multiple rows at once. This is what I did: Iterate the indexes, making a list of dragged items, and setting the old array items to be NSNull. Then insert the items at the designated index. Lastly remove all the NSNulls from the array. NSMutableArray *allItemsArray = [NSMutableArray arrayWithArray:[fileArrayController arrangedObjects]]; NSMutableArray *draggedItemsArray = [NSMutableArray arrayWithCapacity:[rowIndexes count]]; NSUInteger currentItemIndex; NSRange range = NSMakeRange( 0, [rowIndexes lastIndex] + 1 ); while([rowIndexes getIndexes:¤tItemIndex maxCount:1 inIndexRange:&range] > 0) { NSManagedObject *thisItem = [allItemsArray objectAtIndex:currentItemIndex]; [draggedItemsArray addObject:thisItem]; [allItemsArray replaceObjectAtIndex:currentItemIndex withObject:[NSNull null]]; } for (NSInteger i = [draggedItemsArray count]-1; 0 <= i; i--) { NSString *file = [draggedItemsArray objectAtIndex:i]; [allItemsArray insertObject:file atIndex:row]; } [allItemsArray removeObject:[NSNull null]]; [fileArrayController setContent:allItemsArray]; > > 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 PROTECTED] > > http://www.silverscripting.com > > > > > > </div> > > _______________________________________________ > > Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) > > 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: > http://lists.apple.com/mailman/options/cocoa-dev/idou747%40yahoo.com > > This email sent to [EMAIL PROTECTED] Start your day with Yahoo!7 and win a Sony Bravia TV. Enter now http://au.docs.yahoo.com/homepageset/?p1=other&p2=au&p3=tagline _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to [EMAIL PROTECTED]