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?
You can't. NSArray is immutable. You can copy that array into an NSMutableArray (or use that in the first place). The mutable version has methods to move and exchange its items.
@implementation NSMutableArray (Convenience)
- (void)moveObjectAtIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
{
// Optional toIndex adjustment if you think toIndex refers to the position in the array before the move (as per Richard's comment)
if (fromIndex < toIndex) {
toIndex--; // Optional
}
id object = [self objectAtIndex:fromIndex];
[self removeObjectAtIndex:fromIndex];
[self insertObject:object atIndex:toIndex];
}
@end