是什么导致我的应用程序在返回屏幕时崩溃?

what causes my app crashing when coming back to screen?

我在使用自定义 class 的 UITableView 中列出了视频。这是标签栏应用程序。当我向下滚动到第 8 个视频并转到下一个屏幕并返回视频列表屏幕时,应用程序崩溃了。我试图调试但无法找出问题所在。这是我从调试器得到的。

2016-06-08 12:47:46.919 Votocast[2283:453809] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 8 beyond bounds for empty array'
*** First throw call stack:
(
    0   CoreFoundation                      0x0000000108ba9d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x000000010a7f9deb objc_exception_throw + 48
    2   CoreFoundation                      0x0000000108a87804 -[__NSArrayM objectAtIndex:] + 212
    3   Votocast                            0x0000000107dfd4de -[HomeView tableView:cellForRowAtIndexPath:] + 958
    4   UIKit                               0x000000010b6914f4 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 766
    5   UIKit                               0x000000010b69162c -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 74
    6   UIKit                               0x000000010b665c8a -[UITableView _updateVisibleCellsNow:isRecursive:] + 2799
    7   UIKit                               0x000000010b69a686 -[UITableView _performWithCachedTraitCollection:] + 92
    8   UIKit                               0x000000010b681344 -[UITableView layoutSubviews] + 224
    9   UIKit                               0x000000010b5ee980 -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 703
    10  QuartzCore                          0x0000000109ff1c00 -[CALayer layoutSublayers] + 146
    11  QuartzCore                          0x0000000109fe608e _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    12  QuartzCore                          0x0000000109fe5f0c _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    13  QuartzCore                          0x0000000109fda3c9 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    14  QuartzCore                          0x000000010a008086 _ZN2CA11Transaction6commitEv + 486
    15  UIKit                               0x000000010b52e72e _UIApplicationHandleEventQueue + 7135
    16  CoreFoundation                      0x0000000108acf301 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    17  CoreFoundation                      0x0000000108ac522c __CFRunLoopDoSources0 + 556
    18  CoreFoundation                      0x0000000108ac46e3 __CFRunLoopRun + 867
    19  CoreFoundation                      0x0000000108ac40f8 CFRunLoopRunSpecific + 488
    20  GraphicsServices                    0x000000010d4ebad2 GSEventRunModal + 161
    21  UIKit                               0x000000010b533f09 UIApplicationMain + 171
    22  Votocast                            0x0000000107e48d8f main + 111
    23  libdyld.dylib                       0x000000010cc0792d start + 1
    24  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

在 viewWillAppear 中,我从该数组中删除所有对象并调用 webservice。但在我从 web 服务获得响应之前,应用程序崩溃到 cellForRowAtIndexPath 中的一行。您的小小帮助将不胜感激。

编辑:

我注意到只有当我的 tableview decelerationrate 和我转到下一个屏幕并返回到视频列表屏幕时,应用程序才会崩溃。我没有做任何类型的减速代码。

很明显,您的数据源代理工作不正常。如果清空数据源使用的数组,则必须调用 reloadData。当您获得新数据时,您会再次调用 reloadData。

但是,您不应该以这种方式刷新数据。您应该显示旧数据,直到新数据到达。

你在 viewWillAppear 方法中清除了数组中的所有对象但是,你是否在之后重新加载表视图。

因为这可能会导致崩溃。您删除了所有对象,但 tableview 尝试转到最后滚动的索引。所以它导致了崩溃。

希望对您有所帮助。

谢谢你们。我修好了它。我实际上是在 viewDidLoad 中分配数组,只是在 viewWillAppear 中从该数组中删除对象。所以我保留我的数据而不是在 viewWillAppear 中调用 webservice。