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:49:46 -0500
On Tuesday, October 8, 2002, at 02:47 PM, Jonathan E. Jackel wrote:
NSEnumerator *e = [tv selectedRowEnumerator];
[snip]
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.
Another solution is to enumerate backwards, deleting row 3 and then
row 1.
Row 1 stays at row 1 even after you delete row 3, so you don't have to
adjust for the number of deletions you have made. You could probably
(haven't tested this myself) fix the problem with:
NSEnumerator *e = [[[tv selectedRowEnumerator] allObjects]
reverseObjectEnumerator];
Yes, this should work, but I don't think that NSTableView guarantees
that its row enumerator will return the rows in any particular order,
so I always sort them beforehand:
NSEnumerator *e = [[[[tv selectedRowEnumerator] allObjects]
sortedArrayUsingSelector:@selector(compare:)] reverseObjectEnumerator];
--
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.