NSMutablearray move object from index to index

Jonathan. picture Jonathan. · Dec 3, 2010 · Viewed 46.9k times · Source

I have a UItableview with reordable rows and the data is in an NSarray. So how do I move an object in the NSMutablearray when the appropriate tableview delegate is called?

Another way to ask this is how to reorder an NSMutableArray?

Answer

Joost picture Joost · Dec 4, 2010
id object = [[[self.array objectAtIndex:index] retain] autorelease];
[self.array removeObjectAtIndex:index];
[self.array insertObject:object atIndex:newIndex];

That's all. Taking care of the retain count is important, since the array might be the only one referencing the object.