@interface NSMutableArray (MoveObject)
- (void)moveObjectFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex;
@end
@implementation NSMutableArray (MoveObject)
- (void)moveObjectFromIndex:(NSUInteger)fromIndex toIndex:(NSUInteger)toIndex
{
if (fromIndex > toIndex) {
id dummy = [[[NSObject alloc] init] autorelease];
id obj = [self objectAtIndex:fromIndex];
[self insertObject:dummy atIndex:fromIndex];
[self removeObject:obj];
[self insertObject:obj atIndex:toIndex];
[self removeObject:dummy];
}
else if (toIndex > fromIndex) {
id obj = [self objectAtIndex:fromIndex];
[self removeObject:obj];
[self insertObject:obj atIndex:toIndex];
}
}
@end
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath
{
[self.dataList moveObjectFromIndex:sourceIndexPath.row toIndex:destinationIndexPath.row];
}
コメントする