• 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: Row moving [source]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Row moving [source]


  • Subject: Re: Row moving [source]
  • From: mmalcolm crawford <email@hidden>
  • Date: Mon, 2 Feb 2004 00:36:42 -0800

On Feb 1, 2004, at 6:50 PM, Herr Witten wrote:

Here are some useful methods based on Daryn's code, but for an
NSArrayController:

- (void)moveObjectFromIndex: (unsigned)index toIndex: (unsigned)newIndex
{
if (index == newIndex)
return;

NSMutableArray* content = [self content];

[content insertObject: [content objectAtIndex: index] atIndex:
newIndex];

[self removeObjectAtArrangedObjectIndex: (index < newIndex) ? index
: index + 1];
}

This is wrong.
It confuses the content array and the arranged objects. It will not work if the array is filtered. A better implementation would be:

- (void)moveObjectFromIndex: (unsigned)index toIndex: (unsigned)newIndex
{
if (index == newIndex)
return;

NSMutableArray *content = [self content];
id theObject = [content objectAtIndex:index];
[content insertObject:theObject atIndex:newIndex];
[content removeObjectAtIndex: (index < newIndex) ? index
: index + 1];
}

This, though, again, does not work if the array is filtered or re-arranged. To move items in the arranged array, this would be more appropriate:

- (void)moveObjectFromArrangedObjectIndex: (unsigned)index toIndex: (unsigned)newIndex
{
if (index == newIndex)
return;

id theObject = [[self arrangedObjects] objectAtIndex:index];
[self insertObject:theObject atArrangedObjectIndex:newIndex];
[self removeObjectAtArrangedObjectIndex: (index < newIndex) ? index
: index + 1];
}


The latter implementation, however, will not affect the content array. It's not clear to me what the appropriate user experience should be if the array is both filterable and rearrangeable.

See also complete examples at <http://homepage.mac.com/mmalc/CocoaExamples/controllers.html>

mmalc
_______________________________________________
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.

  • Follow-Ups:
    • Re: Row moving [source]
      • From: Herr Witten <email@hidden>
    • Re: Row moving [source]
      • From: Herr Witten <email@hidden>
    • Re: Row moving [source]
      • From: Scott Anguish <email@hidden>
References: 
 >Row moving [source] (From: Herr Witten <email@hidden>)

  • Prev by Date: Re: textfield with percent sign
  • Next by Date: Re: Cocoa/Windows parallel dvlpmt
  • Previous by thread: Row moving [source]
  • Next by thread: Re: Row moving [source]
  • Index(es):
    • Date
    • Thread