user1838169
user1838169

Reputation: 534

iOS reordering a UITableView

I have a UITableView that reads data from a sql db.

When i reorder the data it only changes the top and bottom ones

enter image description here

Dragging 1 from top to bottom

enter image description here

When i let it go at 5 it only switches the top and bottom ones.

enter image description here I am using the following

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {

            [myStringsArray exchangeObjectAtIndex:fromIndexPath.row withObjectAtIndex:toIndexPath.row];

}

How can i reorder the list? So it'll be 2,3,4,5,1 and not 5,2,3,4,1

Upvotes: 1

Views: 979

Answers (1)

Nirav Gadhiya
Nirav Gadhiya

Reputation: 6342

Exchange is not a proper way to do it you should use this.

- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
 {
     id buffer = [myStringsArray objectAtIndex:fromIndexPath.row];
     [myStringsArray removeObjectAtIndex:fromIndexPath.row];  
     [myStringsArray insertObject:buffer atIndex:toIndexPath.row];
}

Edited to match comment below.

Upvotes: 5

Related Questions