UITableView insertRowsAtIndexPaths out of order

Posted by bschlenk on Stack Overflow See other posts from Stack Overflow or by bschlenk
Published on 2010-04-23T18:01:46Z Indexed on 2010/04/23 18:03 UTC
Read the original article Hit count: 730

I currently have a UISegmentedControl set to add/remove table view cells when its value changes. Removing cells works perfectly, however when I insert cells they're in reverse order every other time.

NSArray *addindexes = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:2 inSection:0], [NSIndexPath indexPathForRow:3 inSection:0], [NSIndexPath indexPathForRow:4 inSection:0], nil];
NSArray *removeindexes = [NSArray arrayWithObjects:[NSIndexPath indexPathForRow:2 inSection:0], [NSIndexPath indexPathForRow:3 inSection:0], [NSIndexPath indexPathForRow:4 inSection:0], nil];
[self.tableView beginUpdates];

switch (switchType.selectedSegmentIndex) {
    case 0:
        [self.tableView insertRowsAtIndexPaths:addindexes withRowAnimation:UITableViewRowAnimationTop];
        break;
    case 1:
        [self.tableView deleteRowsAtIndexPaths:removeindexes withRowAnimation:UITableViewRowAnimationTop];

        break;
    default:
        break;
}

[self.tableView endUpdates];}

For example, every other time I add/remove cells they're in reverse order. (4, 3, 2 instead of 2, 3, 4)
1) Remove cells- add cells- correct order
2) Remove cells- add cells- incorrect order
3) Remove cells- add cells- correct order

© Stack Overflow or respective owner

Related posts about iphone

Related posts about uitableview