0

コードに問題があり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];
}

どうすればこれを修正できますか?

4

1 に答える 1

2

問題はデータ ソースにあります。このエラーは、セクションから行を削除しようとしているが、numberOfRowsInSection:メソッドが元の数値から 1 を引いた値を返すべきなのに、同じ数値を返すことを意味します。行を別のセクションに追加する場合も同じです。

于 2012-12-30T11:20:46.867 に答える