gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 22:42:38 24 4
gpt4 key购买 nike

我的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com