删除项目末尾的UITableView时崩溃

时间:2014-07-18 12:07:35

标签: ios uitableview ios8

我有一个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上抛出了一个错误,但出现以下错误。

  

*由于未捕获的异常'NSInvalidArgumentException'而终止应用,原因:'* - [__ NSArrayM   insertObject:atIndex:]:object不能为nil'

现在,为了节省每个人的时间,我已经进行了广泛的测试和验证,我的数据源更新和删除了正确的indexPath,一切都绝对正确。我只是注意到只有当我一直滚动到我的tableview底部以便我不能再滚动,然后删除页面上可见区域中的单元格时才会出现问题。

我可以在我的tableview中的任何其他位置正确删除,只要屏幕下方有更多内容,但是一旦我在tableview的末尾,删除就会抛出此错误。

如果有帮助,我基本上在UITableViewController中有UIPageViewController(普通样式)。我已经尝试删除UIPageViewController并只是推入UITableViewController,但仍然会发生此错误。

有谁知道如何克服这个问题?

编辑:

似乎很多人仍然认为它可能是数组,但这就是事情。我可以在我的tableview中移动我的部分(所有部分都有可以删除的行)。当tableview完全滚动到底部时,我会在删除任何可见部分中的行时发生崩溃。这个代码片段被抽象并在所有部分中使用,所以我很确定代码是正确的。

当我删除tableview位于中间的部分中的行时,我注意到该行会向上滑动以删除,即使我说要使用淡入淡出动画。动画为其下方的行或部分设置动画,这是正确的行为。我觉得这可能与tableview的内容结束时有关,它试图向上滚动,但那里什么也没有。

HACK: 我刚刚实现的一个hack是将一个简单的UIView与tableView.tableFooterView中的tableView相同。这会导致我的tableView具有更多可滚动的大小,我不喜欢这样,但是由于在删除最后一个单元格时有更多可滚动空间向上移动,所以不会发生崩溃。我很感激我无需这样做的解决方案。

嗯,如果页脚视图非常大,它似乎才有效。如果我使页脚视图更小,如100像素,如果滚动到tableview的边缘,它仍然会崩溃。

Stack Trace:(调试器将首先崩溃到上面代码中的endUpdates行,当我再次调试调试器时,这个堆栈跟踪会出现)

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

2 个答案:

答案 0 :(得分:0)

你在调用scrollToRowAtIndexPath吗?这可能导致崩溃。

答案 1 :(得分:0)

我发现将UITableView样式设置为plain而不是分组为我解决了这个问题。您可以通过在故事板中选择表来找到该设置。

https://stackoverflow.com/a/20261510/585227