Drag and Drop Reordering of NSTableView’s Rows
I just had to re-implement drag-and-drop-reordering of Slider cells. I was lucky enough to find my post on the Cocoa-dev list – I just had to copy-paste the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #define MyPrivateTableViewDataType @"NSMutableDictionary" - (void)awakeFromNib { [table registerForDraggedTypes:[NSArray arrayWithObject:MyPrivateTableViewDataType]]; } - (BOOL)tableView:(NSTableView *)tv writeRowsWithIndexes:(NSIndexSet *)rowIndexestoPasteboard:(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]; } } } } |
Posting replies to the lists helps more than just the receiver – keep in mind that source code can be lost. Time is saved when tedious sections can be copy-pasted in during rewriting!
Like this post? Move it on along with:
Email |
delicious |
Digg |
Tweet |
Reddit |
Newsvine |
Furl |
Google |
Stumble |
HaoHao
| Trackback: |
Related posts:
- Rotate NSImage in an NSImageView As soon as I implemented this I realised how much of a pain it would be to copy out the arrow images, rotate them in an external program, then copy them back in. So I put some buttons next to the arrow image wells that allow the user to rotate the arrows within Slider itself. ...
- Free AJAX Loading GIF Generator Using the Ajaxinfo GIF generator, one may choose the colour, background colour or choose transparent background, and the style of the gif. There are over 15 styles to choose from, ranging from rotating circles to simulated loading bars....





























Recent Comments
Arrived In Shanghai
Done, look left!
Sun, 03 Jan 2010 15:37:03 +0000
Arrived In Shanghai
Hi :)
I'll see what I can do!
Sun, 03 Jan 2010 14:23:07 +0000
Arrived In Shanghai
Hi Mike. It would be very useful to have the time date and weather conditions in Shanghai, on your site. Be seeing you soon. Love NZMum.
Sat, 02 Jan 2010 23:30:41 +0000
Js Kit Comments Correct Usage Of The Permalink And Path Attributes
I think it could be better
Thu, 31 Dec 2009 09:01:44 +0000