FROM : Brian Webster
DATE : Tue Oct 08 21:01:23 2002
On Tuesday, October 8, 2002, at 01:32 PM,
<email_removed> 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_removed>
http://homepage.mac.com/bwebster
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
DATE : Tue Oct 08 21:01:23 2002
On Tuesday, October 8, 2002, at 01:32 PM,
<email_removed> 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_removed>
http://homepage.mac.com/bwebster
_______________________________________________
cocoa-dev mailing list | <email_removed>
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
| Related mails | Author | Date |
|---|---|---|
| mw | Oct 8, 15:20 | |
| Brian Webster | Oct 8, 21:01 | |
| Jonathan E. Jackel | Oct 8, 21:47 | |
| Brian Webster | Oct 8, 21:49 | |
| mw | Oct 8, 22:21 | |
| Bill Cheeseman | Oct 9, 12:07 | |
| mw | Oct 10, 01:06 | |
| Bill Cheeseman | Oct 10, 02:43 | |
| mw | Oct 10, 14:13 | |
| Bill Cheeseman | Oct 10, 15:44 |






Cocoa mail archive

