Swift 2:UITableView 'reloadSections' 方法在 iOS 9 中崩溃

Swift 2: UITableView 'reloadSections' method crash in iOS 9

我的应用程序在强制升级到 Swift 2.x 之前运行良好。我下面的代码是在 IBAction 上执行的,用于 "collapses" 单元格的按钮,例如它用另一个布局替换它,然后尝试重新加载单元格,由于各种原因,我被迫将每个单元格包含在它自己的部分中(因此,我正在重新加载该部分)。我试过将各种变量输出到终端;输入是正确的。我也试过将它包装在 .beginUpdates() 和 .endUpdates() 中,当在调试器中遍历它时,它只会在 .endUpdates() 调用而不是 .reloadSections() 调用上抛出以下异常。我还尝试用对 .reloadData() 的全面调用替换 .reloadSections() 并且我收到相同的堆栈跟踪,让我相信它在某种程度上是 TableView 对象的内部。

// Set Selected Patient Configuration to "Collapsed"
patientList.patients[ sender.tag ].updateConfiguration( collapsedConfiguration )
let animation = UITableViewRowAnimation.Automatic

// Retrieve Current Table View and Animate Changes
let tableView = self.superview!.superview as! UITableView

// Reload Section with Animation
tableView.reloadSections(NSIndexSet( index: sender.tag ), withRowAnimation: animation )

我的代码将评估支持数据的折叠状态并在完整视图或折叠视图中绘制它。

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM insertObject:atIndex:]: index 2 beyond bounds [0 .. 0]'
*** First throw call stack:
(
    0   CoreFoundation                      0x00e2fa84 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x03235e02 objc_exception_throw + 50
    2   CoreFoundation                      0x00cdd211 -[__NSArrayM insertObject:atIndex:] + 881
    3   Foundation                          0x01510134 -[NSKeyValueSlowMutableArray insertObject:atIndex:] + 115
    4   CoreFoundation                      0x00d38630 -[NSMutableArray insertObjects:count:atIndex:] + 192
    5   CoreFoundation                      0x00d38322 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 322
    6   CoreFoundation                      0x00d381c2 -[NSMutableArray addObjectsFromArray:] + 834
    7   UIKit                               0x024f4d4f -[UIRuntimeOutletCollectionConnection performConnect] + 964
    8   UIKit                               0x024f4daf -[UIRuntimeOutletCollectionConnection connect] + 33
    9   libobjc.A.dylib                     0x0324a00c -[NSObject performSelector:] + 62
    10  CoreFoundation                      0x00d60571 -[NSArray makeObjectsPerformSelector:] + 273
    11  UIKit                               0x021b06f6 -[UINib instantiateWithOwner:options:] + 2102
    12  UIKit                               0x01eeed6c -[UITableView _dequeueReusableViewOfType:withIdentifier:] + 457
    13  UIKit                               0x01eef173 -[UITableView dequeueReusableCellWithIdentifier:] + 48
    14  Halo                                0x001b5174 _TFC4Halo18MainViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 2356
    15  Halo                                0x001be459 _TToFC4Halo18MainViewController9tableViewfS0_FTCSo11UITableView21cellForRowAtIndexPathCSo11NSIndexPath_CSo15UITableViewCell + 89
    16  UIKit                               0x01f03a19 -[UITableView _createPreparedCellForGlobalRow:withIndexPath:willDisplay:] + 782
    17  UIKit                               0x01f03b47 -[UITableView _createPreparedCellForGlobalRow:willDisplay:] + 90
    18  UIKit                               0x022568cc -[_UITableViewUpdateSupport(Private) _setupAnimationsForNewlyInsertedCells] + 8353
    19  UIKit                               0x0226181a -[_UITableViewUpdateSupport _setupAnimations] + 161
    20  UIKit                               0x01ed7fd3 -[UITableView _updateWithItems:updateSupport:] + 3653
    21  UIKit                               0x01ecfc91 -[UITableView _endCellAnimationsWithContext:] + 17491
    22  UIKit                               0x01eea538 -[UITableView _updateSections:updateAction:withRowAnimation:headerFooterOnly:] + 460
    23  UIKit                               0x01eea625 -[UITableView reloadSections:withRowAnimation:] + 73
    24  Halo                                0x00105e10 _TFC4Halo28FullPatientViewTableViewCell19collapseButtonPressfS0_FCSo8UIButtonT_ + 6208
    25  Halo                                0x0010645d _TToFC4Halo28FullPatientViewTableViewCell19collapseButtonPressfS0_FCSo8UIButtonT_ + 61
    26  libobjc.A.dylib                     0x0324a0b5 -[NSObject performSelector:withObject:withObject:] + 84
    27  UIKit                               0x01d8016a -[UIApplication sendAction:to:from:forEvent:] + 118
    28  UIKit                               0x01d800e9 -[UIApplication sendAction:toTarget:fromSender:forEvent:] + 64
    29  UIKit                               0x01f1e19f -[UIControl sendAction:to:forEvent:] + 79
    30  UIKit                               0x01f1e51f -[UIControl _sendActionsForEvents:withEvent:] + 408
    31  UIKit                               0x01f1d525 -[UIControl touchesEnded:withEvent:] + 714
    32  UIKit                               0x022efeaa _UIGestureRecognizerUpdate + 12763
    33  UIKit                               0x01dfabdf -[UIWindow _sendGesturesForEvent:] + 1559
    34  UIKit                               0x01dfc298 -[UIWindow sendEvent:] + 1137
    35  UIKit                               0x01da1efb -[UIApplication sendEvent:] + 266
    36  UIKit                               0x01d77cb6 _UIApplicationHandleEventQueue + 7802
    37  CoreFoundation                      0x00d49d1f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
    38  CoreFoundation                      0x00d3f9ab __CFRunLoopDoSources0 + 523
    39  CoreFoundation                      0x00d3edc8 __CFRunLoopRun + 1032
    40  CoreFoundation                      0x00d3e706 CFRunLoopRunSpecific + 470
    41  CoreFoundation                      0x00d3e51b CFRunLoopRunInMode + 123
    42  GraphicsServices                    0x058c0664 GSEventRunModal + 192
    43  GraphicsServices                    0x058c04a1 GSEventRun + 104
    44  UIKit                               0x01d7e1eb UIApplicationMain + 160
    45  Halo                                0x0011eb6c main + 140
    46  libdyld.dylib                       0x03ceca21 start + 1
    47  ???                                 0x00000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

堆栈对我不是很有帮助,我什至无法弄清楚开头的信息:"insertObject:atIndex:]: index 2 beyond bounds [0 .. 0]." 没有在这个示例和我的测试中只有一个 table 条目的地方应该有索引为“2”。如果您能提供任何帮助,我们将不胜感激。

这是我对这个例外的 'bt':

* thread #1: tid = 0xc9ca7, 0x0401969a libsystem_kernel.dylib`__pthread_kill + 10, queue = 'com.apple.main-thread', stop reason = signal SIGABRT
    frame #0: 0x0401969a libsystem_kernel.dylib`__pthread_kill + 10
    frame #1: 0x03fe4634 libsystem_pthread.dylib`pthread_kill + 101
    frame #2: 0x03d9339d libsystem_c.dylib`abort + 156
    frame #3: 0x04034ae7 libc++abi.dylib`abort_message + 151
    frame #4: 0x04055683 libc++abi.dylib`default_terminate_handler() + 300
    frame #5: 0x032360b6 libobjc.A.dylib`_objc_terminate() + 108
    frame #6: 0x04052d0d libc++abi.dylib`std::__terminate(void (*)()) + 14
    frame #7: 0x0405293e libc++abi.dylib`__cxa_rethrow + 103
    frame #8: 0x03235fad libobjc.A.dylib`objc_exception_rethrow + 47
    frame #9: 0x00d3e7d0 CoreFoundation`CFRunLoopRunSpecific + 672
    frame #10: 0x00d3e51b CoreFoundation`CFRunLoopRunInMode + 123
    frame #11: 0x058c0664 GraphicsServices`GSEventRunModal + 192
    frame #12: 0x058c04a1 GraphicsServices`GSEventRun + 104
    frame #13: 0x01d7e1eb UIKit`UIApplicationMain + 160
  * frame #14: 0x0011eb6c Halo`main + 140 at AppDelegate.swift:26
    frame #15: 0x03ceca21 libdyld.dylib`start + 1

根据请求,这是输出后的代码:

    // Retrieve Current Table View and Animate Changes
    let tableView = self.superview!.superview as! UITableView

    // Test Output
    print( "tableView.numberOfSections: \(tableView.numberOfSections)" )
    print( "tableView.numberOfRowsInSection( sender.tag ): \(tableView.numberOfRowsInSection( sender.tag ))" )

    // Reload Section with Animation
    tableView.reloadSections(NSIndexSet( index: sender.tag ), withRowAnimation: animation )

    tableView.numberOfSections: 1
    tableView.numberOfRowsInSection( sender.tag ): 1

堆栈跟踪的这一部分(可能)意味着您有一个 IBOutletCollection,并且 UIKit 现在正在尝试从故事板或笔尖填充它:

7   UIKit                               0x024f4d4f -[UIRuntimeOutletCollectionConnection performConnect] + 964
8   UIKit                               0x024f4daf -[UIRuntimeOutletCollectionConnection connect] + 33

这部分看起来像是在尝试将绑定对象添加到现有数组:

2   CoreFoundation                      0x00cdd211 -[__NSArrayM insertObject:atIndex:] + 881
3   Foundation                          0x01510134 -[NSKeyValueSlowMutableArray insertObject:atIndex:] + 115
4   CoreFoundation                      0x00d38630 -[NSMutableArray insertObjects:count:atIndex:] + 192
5   CoreFoundation                      0x00d38322 -[NSMutableArray insertObjectsFromArray:range:atIndex:] + 322
6   CoreFoundation                      0x00d381c2 -[NSMutableArray addObjectsFromArray:] + 834

所有这些都意味着该错误可能与您发布的代码无关,而是与您的 nib 以及它如何与绑定的属性交互有关。

看看你是否在使用 IBOutletCollection,如果是,开始调试它绑定的东西。