Rzeczywiście robię coś podobnego w przypadku jednej z moich aplikacji. Wykorzystuje metody delegatów do edycji tabeli i trochę "podstępu" użytkownika. Wbudowana w 100% funkcjonalność Apple.
1 - Ustaw tabelę do edycji (robię to w viewWillAppear)
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.tableView setEditing:YES];
}
2 - ukryć ikonę domyślną akcesorium:
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView
editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
//remove any editing accessories (AKA) delete button
return UITableViewCellAccessoryNone;
}
3 - Zachowaj edycji tryb przeniesienia wszystkiego w prawo (w komórce)
-(BOOL)tableView:(UITableView *)tableView shouldIndentWhileEditingRowAtIndexPath:(NSIndexPath *)indexPath{
return NO;
}
4 - W tym momencie powinieneś być w stanie przeciągnąć komórki wokół siebie, nie wyglądając, jakby były w trybie edycji. Tutaj oszukujemy użytkownika. Stwórz własną ikonę "przenieś" (trzy linie w domyślnym przypadku, dowolna ikona w twoim przypadku) i dodaj obrazek w prawo tam, gdzie normalnie trafi na komórkę.
5 - Wreszcie, zaimplementuj metodę delegatów, aby zmienić rzeczywiste źródło danych.
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)sourceIndexPath toIndexPath:(NSIndexPath *)destinationIndexPath{
//Get original id
NSMutableArray *originalArray = [self.model.items objectAtIndex:sourceIndexPath.section];
Item * original = [originalArray objectAtIndex:sourceIndexPath.row];
//Get destination id
NSMutableArray *destinationArray = [self.model.items objectAtIndex:destinationIndexPath.section];
Item * destination = [destinationArray objectAtIndex:destinationIndexPath.row];
CGPoint temp = CGPointMake([original.section intValue], [original.row intValue]);
original.row = destination.row;
original.section = destination.section;
destination.section = @(temp.x);
destination.row = @(temp.y);
//Put destination value in original array
[originalArray replaceObjectAtIndex:sourceIndexPath.row withObject:destination];
//put original value in destination array
[destinationArray replaceObjectAtIndex:destinationIndexPath.row withObject:original];
//reload tableview smoothly to reflect changes
dispatch_async(dispatch_get_main_queue(), ^{
[UIView transitionWithView:tableView duration:duration options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[tableView reloadData];
} completion:NULL];
});
}
Może spróbować metody w tym ostatnim artykule: http: //www.raywenderlich.com/63089/cookbook-moving-table-view-cells-with-a-long-press-gesture – Jack
To może pracować i wiem, że ta metoda jest możliwa. Ale moje pytanie brzmi: jak to zrobić, używając domyślnego kodu ruchu komórki. –
@AleksanderAzizi Czy mogę zapytać, dlaczego boisz się trybu edycji? –