objective-c - 在部分之间移动一行

标签 objective-c ios uitableview

我的 moveRowAtIndexPath:toIndexPath 代码有问题。

当我在一个部分内移动单元格但当我尝试从一个部分移动一行时(比如从 indexPath [1,1]indexPath [2, 0]) 到另一个我收到此错误消息时崩溃:

* Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1. The number of rows contained in an existing section after the update (2) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 1 moved out).'

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

我该如何解决这个问题?

最佳答案

问题出在您的数据源上。该错误意味着您正在从一个部分中删除一行,但 numberOfRowsInSection: 方法返回相同的数字,而它应该返回旧数字减 1。将行添加到其他部分也是如此。

关于objective-c - 在部分之间移动一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14090194/

相关文章:

ios - 如何基于UIImageView高度设置不同的UITableViewCell高度?

ios - UITableview 向上滚动 UISectionHeader 的一半

ios - 第二次使用一个方法给我一个错误(IOS)

ios - UICollectionView 在 iOS 7 上运行时不显示

IOS 7如何打印url响应数据

ios - 使用多个 View Controller 堆栈处理方向

ios - `UIApplication.shared.canOpenURL(:)` 方法不适用于 iOS 11.4(仅限 iPad 设备)?

android - 以网关为中心的 BLE 多连接应用程序

swift - UITableViewController 和预取

ios - Storyboard非法配置 iOS 7