Re: NSArray - move Items at indexes
Re: NSArray - move Items at indexes
- Subject: Re: NSArray - move Items at indexes
- From: Gordon Apple <email@hidden>
- Date: Fri, 21 Sep 2007 14:54:35 -0500
One thing that might be considered, I don't think he mentioned if he was
using an NSArrayController or a subclass of it. I recently snatched a
similar routine for my controller subclass from the Bookmarks sample code:
-(void) moveObjectsInArrangedObjectsFromIndexes:(NSIndexSet*)indexSet
toIndex:(unsigned int)insertIndex
One thing I've learned is that if you are using array controllers and
bindings with table views: DONT MESS WITH THE ARRAY DIRECTLY! Do it through
the controller methods instead if you want the changes to track in other
views. I have several views that differently display the same info. I
guess if you know what you are doing with notifications (I don't) you can
make it work directly. It may or may not apply in his case.
> -(void) moveObjectsAtIndexes:(NSindexSet*)indexes toIndex:(unsigned)index
> {
> NSArray *objectsToMove = [self objectsAtIndexes: indexes];
>
> //If any of the removed objects come before the index, we want to
> decrement the index appropriately
> index -= [indexes countOfIndexesInRange: (NSRange){0, index}];
>
> [self removeObjectsAtIndexes: indexes];
> [self replaceObjectsInRange: (NSRange){index,0}
> withObjectsFromArray: objectsToMove];
> }
>
> ... much cleaner.
>
_______________________________________________
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