我的moveRowAtIndexPath:toIndexPath
代码有问题。
当我在一个部分中移动单元格时它工作正常,但是当我尝试将一行从一个部分(比如 from indexPath [1,1]
to indexPath [2,0]
)移动到另一个部分时,我收到以下错误消息的崩溃:
*由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效更新:第 1 节中的行数无效。更新 (2) 后现有节中包含的行数必须等于其中包含的行数更新前的那个节 (2),加上或减去从该节插入或删除的行数(0 插入,0 删除),加上或减去移入或移出该节的行数(0 移入,1搬出)。'
这是我的代码,如果两个 indexPaths 共享相同的节号,它可以正常工作:
- (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];
}
我怎样才能解决这个问题?