我有一个带有一堆节/行的UITableView。我一直被一个奇怪的错误所困扰,在该行中,当我删除一行时,我的程序将崩溃。我的删除代码如下:
[self.tableView beginUpdates];
NSMutableArray* myArray = [self.myArray mutableCopy];
[myArray removeObjectAtIndex:indexPath.row];
self.myArray = myArray;
[self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[self.tableView endUpdates];
调试器在
endUpdates
上向我抛出错误,并显示以下错误。现在,为了节省大家的时间,我已经进行了广泛的测试和验证,证明我的数据源更新和删除正确的indexPath都绝对正确。我只是注意到,仅当我一直滚动到表格 View 的底部以致无法再滚动,然后删除页面可见区域中的单元格时,才会出现此问题。
只要在其下方的屏幕外还有更多内容,我就可以正确删除该表 View 中的其他任何位置,但是一旦我处于该表 View 的末尾,删除操作将引发此错误。
如果有帮助,我基本上在
UITableViewController
中有一个UIPageViewController
(纯风格)。我尝试删除UIPageViewController并仅推送到UITableViewController上,但是仍然会发生此错误。有谁知道我能克服这个问题吗?
编辑:
似乎很多人仍在思考它可能是数组,但这就是问题。我可以在表格 View 中移动各节(所有节都有可以删除的行)。当tableview完全滚动到底部时,删除任何可见部分中的行时,我总是会崩溃。该代码段是抽象的,并在所有部分中都使用过,因此我很确定代码是正确的。
当我在表格 View 位于中间居中的部分中删除一行时,我注意到该行向上滑动即可删除,即使我说要使用淡入淡出动画也是如此。动画会对其下方的行或部分进行动画处理,这是正确的行为。我认为这可能与tableview位于其内容的末尾有关,它试图向上滚动某些内容,但是那里什么也没有。
HACK:
我现在实现的一个技巧是在tableView.tableFooterView中放置一个与tableView相同大小的普通UIView。这使我的tableView具有更大的可滚动大小,这是我不喜欢的,但是使用它,由于在删除最后一个单元格时有更多的可滚动空间向上移动,因此不会发生崩溃。我很感激不需要这样做的解决方案。好吧,只有在页脚 View 很大的情况下,它才似乎起作用。如果将页脚 View 缩小,例如100像素,则滚动到表 View 的边缘仍会崩溃。
Stack Trace:
(调试器将首先崩溃到上面代码中的endUpdates行,当我在调试器上再按两次play时,将出现此堆栈跟踪)2014-07-18 05:52:41.314 ScriptChart[26419:905949] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[__NSArrayM insertObject:atIndex:]: object cannot be nil'
*** First throw call stack:
(
0 CoreFoundation 0x0000000109c9f055 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010d1d8a1c objc_exception_throw + 45
2 CoreFoundation 0x0000000109b6d92a -[__NSArrayM insertObject:atIndex:] + 954
3 UIKit 0x000000010b86c424 __46-[UITableView _updateWithItems:updateSupport:]_block_invoke915 + 177
4 UIKit 0x000000010b803a4c +[UIView(UIViewAnimationWithBlocks) _setupAnimationWithDuration:delay:view:options:factory:animations:start:animationStateGenerator:completion:] + 473
5 UIKit 0x000000010b803ca1 +[UIView(UIViewAnimationWithBlocks) animateWithDuration:delay:options:animations:completion:] + 57
6 UIKit 0x000000010b86bfc3 -[UITableView _updateWithItems:updateSupport:] + 2803
7 UIKit 0x000000010b8660b2 -[UITableView _endCellAnimationsWithContext:] + 11934
8 ScriptChart 0x000000010863f272 -[SCLabListNoteSection deleteCell:] + 1250
9 ScriptChart 0x00000001085e127f __45-[SCLabPanelTableViewCell infoButtonPressed:]_block_invoke127 + 207
10 UIKit 0x000000010b9e0fe7 -[UIAlertController _fireOffActionOnTargetIfValidForAction:] + 55
11 UIKit 0x000000010b9e14d5 __85-[UIAlertController _dismissAnimated:triggeringAction:triggeredByPopoverDimmingView:]_block_invoke + 30
12 UIKit 0x000000010b892bc4 -[UIPresentationController transitionDidFinish:] + 1118
13 UIKit 0x000000010b894d8e __56-[UIPresentationController runTransitionForCurrentState]_block_invoke_2 + 133
14 UIKit 0x000000010bec4123 -[_UIViewControllerTransitionContext completeTransition:] + 110
15 UIKit 0x000000010b8037fd -[UIViewAnimationBlockDelegate _didEndBlockAnimation:finished:context:] + 326
16 UIKit 0x000000010b7ec42a -[UIViewAnimationState sendDelegateAnimationDidStop:finished:] + 209
17 UIKit 0x000000010b7ec760 -[UIViewAnimationState animationDidStop:finished:] + 76
18 QuartzCore 0x000000010b4dff9e _ZN2CA5Layer23run_animation_callbacksEPv + 308
19 libdispatch.dylib 0x000000010d537d64 _dispatch_client_callout + 8
20 libdispatch.dylib 0x000000010d523f82 _dispatch_main_queue_callback_4CF + 941
21 CoreFoundation 0x0000000109c07ae9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
22 CoreFoundation 0x0000000109bca46b __CFRunLoopRun + 2043
23 CoreFoundation 0x0000000109bc9a06 CFRunLoopRunSpecific + 470
24 GraphicsServices 0x000000010db6cabf GSEventRunModal + 161
25 UIKit 0x000000010b792cf8 UIApplicationMain + 1282
26 ScriptChart 0x00000001085ca153 main + 179
27 libdyld.dylib 0x000000010d56c145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
最佳答案
您在调用scrollToRowAtIndexPath吗?这可能是导致崩溃的原因。
关于ios - 删除结尾的项目时,UITableView崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24824646/