我使用以下代码重新排序我的 TableViewCells:
- (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];
}
一切都很好,除非您离开视图并返回它。如果我移动单元格,当视图恢复时,它们都按照原来的顺序排列。这有什么原因吗?谢谢您的帮助!