私も同じ問題を抱えていましたが、解決策を見つけたと思います。私のテストでは、1つのセクション(2〜約12個のセルでテスト済み)で正常に機能しますが、異なるセクション間でセルをドラッグする場合は、いくつかの変更が必要になります。その機能は必要ないので、まだこれらの変更を加えていません。
このメソッドは、デリゲートメソッドを実装していることを前提としています
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
セルの初期背景を設定し、正しく機能していることを確認します。
このメソッドは、セルが移動するときに新しい位置に合わせてセルを更新します
- (NSIndexPath *)tableView:(UITableView *)tableView targetIndexPathForMoveFromRowAtIndexPath:(NSIndexPath *)sourceIndexPath toProposedIndexPath:(NSIndexPath *)proposedDestinationIndexPath {
// Get the index paths of the cells above and below our current target cell
NSIndexPath *higherPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row - 1 inSection:proposedDestinationIndexPath.section];
NSIndexPath *lowerPath = [NSIndexPath indexPathForRow:proposedDestinationIndexPath.row + 1 inSection:proposedDestinationIndexPath.section];
// Update our source cell to the appropriate image for its current position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:sourceIndexPath] forRowAtIndexPath:proposedDestinationIndexPath];
if (sourceIndexPath.row > proposedDestinationIndexPath.row) {
// Source cell is below the target
// Update the cell that was in our target position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:proposedDestinationIndexPath] forRowAtIndexPath:lowerPath];
// Ensure that any cell above our new target is refreshed if the cell is dragged back down the list
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:higherPath] forRowAtIndexPath:higherPath];
} else if (sourceIndexPath.row < proposedDestinationIndexPath.row) {
// Source cell is above the target
// Update the cell that was in our target position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:proposedDestinationIndexPath] forRowAtIndexPath:higherPath];
// Ensure that any cell below our new target is refreshed if the cell is dragged back up the list
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:lowerPath] forRowAtIndexPath:lowerPath];
} else {
// Source cell is the same as the target - cell is back in its original position
// Update any cells below and above the position
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:higherPath] forRowAtIndexPath:higherPath];
[self tableView:self.tableView willDisplayCell:[tableView cellForRowAtIndexPath:lowerPath] forRowAtIndexPath:lowerPath];
}
return proposedDestinationIndexPath;
}
私はインフルエンザに苦しんでいるときにこれを思いついたので、説明にいくつかのばかげた間違いをしたかもしれませんが、単一のセクションの制限を除いて、これは確実に機能するはずです。