コードに問題がありmoveRowAtIndexPath:toIndexPath
ます。
セクション内でセルを移動している場合は正常に動作しますが、行をあるセクションから別のセクションに移動しようとすると (たとえば からindexPath [1,1]
) indexPath [2,0]
、次のエラー メッセージでクラッシュします。
*キャッチされない例外 'NSInternalInconsistencyException' が原因でアプリを終了します。理由: '無効な更新: セクション 1 の行数が無効です。更新 (2) 後の既存のセクションに含まれる行数は、更新前のそのセクション (2)、そのセクションから挿入または削除された行の数 (0 挿入、0 削除) のプラスまたはマイナス、およびそのセクションに移動された、またはそのセクションから移動された行の数 (0 移動、1引っ越した)。'
これが私のコードです。2 つの indexPath が同じセクション番号を共有していれば問題なく動作します。
- (void)moveRowAtIndexPath:(NSIndexPath *)indexPath toIndexPath:(NSIndexPath *)newIndexPath
{
NSMutableArray *cells = [[self cellsAtSectionOfIndexPath:indexPath] mutableCopy];
Cell *cell = [self dataAtIndexPath:indexPath];
Cell *newCell = [self dataAtIndexPath:newIndexPath];
if ([indexPath section] == [newIndexPath section])
{
[cells replaceObjectAtIndex:indexPath.row withObject:newCell];
[cells replaceObjectAtIndex:newIndexPath.row withObject:cell];
[self replaceCellsAtIndexPath:indexPath withCells:cells];
}
else
{
NSMutableArray *newCells = [[self cellsAtSectionOfIndexPath:newIndexPath] mutableCopy];
[cells replaceObjectAtIndex:indexPath.row withObject:newCell];
[newCells replaceObjectAtIndex:newIndexPath.row withObject:cell];
[self replaceCellsAtIndexPath:indexPath withCells:cells];
[self replaceCellsAtIndexPath:newIndexPath withCells:newCells];
}
[super moveRowAtIndexPath:indexPath toIndexPath:newIndexPath];
}
どうすればこれを修正できますか?