• 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: NSArray - move Items at indexes
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: NSArray - move Items at indexes
      • From: Chris Hanson <email@hidden>
    • Re: NSArray - move Items at indexes
      • From: Milo Bird <email@hidden>
  • Prev by Date: Re: Parsing HTTP headers
  • Next by Date: Re: User Installed Resouces (Admin Access)
  • Previous by thread: Re: NSArray - move Items at indexes
  • Next by thread: Re: NSArray - move Items at indexes
  • Index(es):
    • Date
    • Thread