• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSTableView Drag-Drop re-ordering...
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSTableView Drag-Drop re-ordering...


  • Subject: Re: NSTableView Drag-Drop re-ordering...
  • From: Chris Idou <email@hidden>
  • Date: Wed, 26 Nov 2008 02:24:33 -0800 (PST)

--- On Wed, 26/11/08, Michael Robinson <email@hidden> 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:&currentItemIndex 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@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


      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 (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

References: 
 >Re: NSTableView Drag-Drop re-ordering... (From: Michael Robinson <email@hidden>)

  • Prev by Date: Re: NSTableView Drag-Drop re-ordering...
  • Next by Date: Re: Preventing a user from moving a window
  • Previous by thread: Re: NSTableView Drag-Drop re-ordering...
  • Next by thread: Newbie question: Object as input arguments
  • Index(es):
    • Date
    • Thread