在部分之间移动一行

时间:2012-12-30 11:06:56

标签: objective-c ios uitableview

我的moveRowAtIndexPath:toIndexPath代码存在问题。

当我在一个部分中移动单元格但是当我尝试从一个部分(例如从indexPath [1,1]indexPath [2,0])移动一行到另一个部分时,它工作得很好我遇到了这个错误的崩溃消息:

  

* 由于未捕获的异常'NSInternalInconsistencyException'而终止应用,原因:'无效更新:无效   第1节中的行数。包含在中的行数   更新后的现有部分(2)必须等于数量   更新前的该部分中包含的行(2),加号或减号   从该部分插入或删除的行数(插入0,   删除0)加上或减去移入或移出的行数   该部分(0移入,1移出)。'

这是我的代码,如果两个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];
}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:2)

问题在于您的数据源。该错误意味着您要从一个部分中删除一行,但numberOfRowsInSection:方法返回相同的数字时应返回旧的数字减去1.将该行添加到另一部分时也是如此。

相关问题