UITableView Swift中的断言失败

时间:2019-01-29 11:16:02

标签: ios swift uitableview

我在相当大的iOS项目(肯定有15个以上的表格视图)中遇到此错误。所以我的问题是,我怎么知道哪个tableView给我这个错误?可能吗? Xcode始终在AppDelegate中显示该错误。

完整的错误描述:

2019-01-29 12:24:24.189428+0100 OrdersApp[28886:8731092] *** Assertion failure in -[UITableView _endCellAnimationsWithContext:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore/UIKit-3698.94.10/UITableView.m:2062
2019-01-29 12:24:24.190710+0100 OrdersApp[28886:8731092] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'
*** First throw call stack:
(0x234fa0ec4 0x234171a40 0x234eb6b3c 0x2359a51d0 0x2624762c8 0x26248e614 0x26248ea20 0x1023b6c84 0x102399fa0 0x103d9b824 0x103d9cdc8 0x103daaa78 0x234f30df4 0x234f2bcbc 0x234f2b1f0 0x2371a4584 0x262282d40 0x10246a4b0 0x2349eabb4)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

2 个答案:

答案 0 :(得分:0)

Hmm为每个表添加一个标签: tableView.tag = 0

现在,您可以在日志“ po tableView.tag”中对此进行调试,也可以在委托中写入print(“ BLA:”,tableView.tag“)。

答案 1 :(得分:0)

如此处所述:

2019-01-29 12:24:24.190710+0100 OrdersApp[28886:8731092] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid update: invalid number of rows in section 1.  The number of rows contained in an existing section after the update (1) must be equal to the number of rows contained in that section before the update (2), plus or minus the number of rows inserted or deleted from that section (0 inserted, 0 deleted) and plus or minus the number of rows moved into or out of that section (0 moved in, 0 moved out).'

您可能从数据源中删除了一些数据,却忘记了删除相应的UITableViewCell,然后再结束tableView更新。

您应该按顺序执行以下操作:

tableView.beginUpdates()
dataSource.remove(at: <#index#>)
tableView.deleteRows(at: [<#indexPath#>], with: .automatic)
tableView.endUpdates()

UIKit引发的所有异常都可以被异常断点捕获。它会指向崩溃之前的一点,您可以检测到它。

Exception break point

别忘了尝试模拟器。捕获此类异常将非常有用。