Re: NSTableView Drag & Drop... Well, Maybe
Re: NSTableView Drag & Drop... Well, Maybe
- Subject: Re: NSTableView Drag & Drop... Well, Maybe
- From: Brian Webster <email@hidden>
- Date: Tue, 8 Oct 2002 14:01:23 -0500
On Tuesday, October 8, 2002, at 01:32 PM,
email@hidden wrote:
- (BOOL)tableView:(NSTableView *)tv acceptDrop:(id
<NSDraggingInfo>)info
row:(int)row dropOperation:(NSTableViewDropOperation)op
{
int i=0;
NSArray *unarchivedPeople;
NSMutableArray *peopleToRemove = [[NSMutableArray alloc] init];
NSPasteboard *pboard = [info draggingPasteboard];
NSData *archivedArray;
NSEnumerator *e = [tv selectedRowEnumerator];
NSNumber *index = [NSNumber numberWithInt:0];
NSString *type = [pboard availableTypeFromArray:[NSArray
arrayWithObject:MPWTableViewPboardType]];
// Check if the type is correct
if(type == MPWTableViewPboardType) {
// Gets all of the selected rows from the table and places
their
objects into the array
// of people to remove (because we are moving, not copying).
while(index = [e nextObject]) {
[employees removeObjectAtIndex:[index intValue]];
}
There is a problem with this loop here. Each time you remove an object
from the employees array, the index of all the employees after the one
that is removed will shift down by one. Then, when you go to the next
selected row, it will no longer refer to the same person.
For example, if rows 1 and 3 are selected, removing row 1 will shift
the employee who was at row 3 down to row 2. Then, when you remove the
employee that is _now_ at row 3, you're actually removing the employee
that _was_ at row 4. You'll have to adjust your indices as you remove
each person in order to compensate for this.
// The type is correct, so we can get and unarchive the NSData
object
archivedArray = [pboard dataForType:MPWTableViewPboardType];
unarchivedPeople = [NSUnarchiver
unarchiveObjectWithData:archivedArray];
// Now we add this unarchived array to the employees array
e = [tv selectedRowEnumerator];
while(index = [e nextObject]) {
[employees insertObject:[unarchivedPeople objectAtIndex:i]
atIndex:[index intValue]];
i+=1;
}
If you want to insert the employees at a single point in the table, you
don't want to iterate the selected rows again. You instead want to use
the "row" argument that gets passed into the method to determine where
to drop. Start by inserting the first person at row, and then do a
row++ each time through the loop to insert each person at the next row
after the first.
--
Brian Webster
email@hidden
http://homepage.mac.com/bwebster
_______________________________________________
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.