Im reordering my TableViewCells with the following code:
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath{
[self.ammoTable moveRowAtIndexPath:indexPath toIndexPath:[NSIndexPath indexPathForRow:[self.tableArray count] - 1 inSection:1]];
}
-(IBAction)setEdit{
if ([self.button.currentTitle isEqualToString:@"Edit"]) {
self.ammoTable.editing = YES;
[self.button setTitle:@"Done" forState:UIControlStateNormal];
}
else{
self.ammoTable.editing = NO;
[self.button setTitle:@"Edit" forState:UIControlStateNormal];
}
}
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
// fetch the object at the row being moved
NSString *r = [self.tableArray objectAtIndex:fromIndexPath.row];
// remove the original from the data structure
[self.tableArray removeObjectAtIndex:fromIndexPath.row];
// insert the object at the target row
[self.tableArray insertObject:r atIndex:toIndexPath.row];
[self.ammoTable reloadData];
}
Everything works great, except when you leave the view and return to it. If i move the cells around, when the view comes back up, they are all in the original order that they were in. Is there any reason for this? Thanks for the help!
because you are reloading your tableview with old array i.e. not with updated array
that means you are reinitializing your array ,it may be in viewDidLoad.
create & initialize your array in appDelegate .Use this array & make changes when you moves cell,it will persist order of elements.
see this link to how to create & access array from appDelegate