隐藏UITableViewCells时将其隐藏起来会崩溃

时间:2015-09-11 00:58:42

标签: ios swift uitableview

我有一个包含自定义tableview单元格的表,它们上面有一个UISwitch。当我点击我称之为“父”单元格的开关时,它的“子”单元格将从表中消失。当我再次点击开关时,它的孩子会回来。

通过让开关在底层数据模型对象(子节点)中设置“enabled”标志,我可以完美地工作。注意,子节点和父节点都是相同的类型,并且是同一个数组的一部分。 heightForRowAtIndexPath方法检查indexPath中的模型对象是否为子对象,如果是,如果它被禁用,则返回0,否则返回正确的非隐藏单元格高度(44)。

我想设置隐藏和显示子单元格的动画,所以我知道我需要在tableView上调用.beginUpdates().endUpdates(),但我不知道在哪里这样做。

当我将它们放在cellForRowAtIndexPath中时,就在返回已配置的出列单元格之前,当我隐藏单元格时它会起作用,但是当我试图将它们带回来时它会崩溃。错误文本如下。

似乎通过将高度设置为0,单元格不再存在,并且索引混乱。有谁知道我应该做些什么来解决这个问题?

谢谢!

错误文字:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndex:]: index 19 beyond bounds [0 .. 18]'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010fe7b9b5 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000111ca0deb objc_exception_throw + 48
    2   CoreFoundation                      0x000000010fd5f514 -[__NSArrayM objectAtIndex:] + 212
    3   UIKit                               0x00000001109df87a -[UITableView _updateVisibleCellsNow:isRecursive:] + 4765
    4   UIKit                               0x0000000110a11fa8 -[UITableView _performWithCachedTraitCollection:] + 92
    5   UIKit                               0x00000001109fa599 -[UITableView layoutSubviews] + 218
    6   UIKit                               0x000000011096ba3b -[UIView(CALayerDelegate) layoutSublayersOfLayer:] + 710
    7   QuartzCore                          0x000000011584e36a -[CALayer layoutSublayers] + 146
    8   QuartzCore                          0x0000000115842bd0 _ZN2CA5Layer16layout_if_neededEPNS_11TransactionE + 366
    9   QuartzCore                          0x0000000115842a4e _ZN2CA5Layer28layout_and_display_if_neededEPNS_11TransactionE + 24
    10  QuartzCore                          0x00000001158371d5 _ZN2CA7Context18commit_transactionEPNS_11TransactionE + 277
    11  QuartzCore                          0x00000001158649f0 _ZN2CA11Transaction6commitEv + 508
    12  QuartzCore                          0x0000000115865154 _ZN2CA11Transaction17observer_callbackEP19__CFRunLoopObservermPv + 92
    13  CoreFoundation                      0x000000010fda7457 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    14  CoreFoundation                      0x000000010fda73c7 __CFRunLoopDoObservers + 391
    15  CoreFoundation                      0x000000010fd9d01b __CFRunLoopRun + 1147
    16  CoreFoundation                      0x000000010fd9c918 CFRunLoopRunSpecific + 488
    17  GraphicsServices                    0x0000000115301ad2 GSEventRunModal + 161
    18  UIKit                               0x00000001108be99e UIApplicationMain + 171
    19  MyApp            0x000000010f82284d main + 109
    20  libdyld.dylib                       0x00000001127cc92d start + 1
    21  ???                                 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

0 个答案:

没有答案
相关问题