次のコードで 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];
}
ビューを離れてビューに戻る場合を除いて、すべてがうまく機能します。セルを移動すると、ビューが元に戻ったときに、それらはすべて元の順序になっています。これには何か理由がありますか? 助けてくれてありがとう!