首页 > 解决方案 > 调用 dequeueReusableCellWithReuseIdentifier:forIndexPath 时由于未捕获的异常“NSRangeException”而终止应用程序:

问题描述

我们遇到了崩溃,我们只能从崩溃日志中看到这些:

2018-11-02 10:15:01.744674-0400 Flights[385:17563] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x18555ed8c 0x1847185ec 0x1854f7750 0x1854e48ac 0x18f266b04 0x18f266084 0x18f265fd8 0x18f265eb8 0x18f2859dc 0x18f286c00 0x18f286a2c 0x100b1c304 0x100ea8b6c 0x100eaa24c 0x100eaa30c 0x100eaa680 0x185566580 0x185445748 0x18544a56c 0x10573e0c8 0x1855642d4 0x18544a41c 0x18f266f60 0x18f266084 0x18f265fd8 0x18f265eb8 0x18f265500 0x18f264730 0x18f133770 0x1896d525c 0x1896d93ec 0x189645aa0 0x18966d5d0 0x18966e450 0x185506910 0x185504238 0x185504884 0x185424da8 0x187407020 0x18f40578c 0x100b8c760 0x184eb5fc0)
libc++abi.dylib: terminating with uncaught exception of type NSException

我们启用了所有异常断点,它将我们指向这行代码:

UICollectionViewCell* cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:path];

一件非常奇怪的事情是,我们只能在 iOS 11.3 设备中重现这一点。但是我们所有的其他测试设备都可以正常工作。

我们几乎被困住了。有人遇到过这种情况并知道任何解决方法吗?

标签: iosobjective-cios11

解决方案


不确定这是否是 iOS 11 中的错误,但由于以下代码尝试将单元格出列以调整大小collectionView:collectionViewLayout:sizeForItemAt:并在 iOS 12 及更高版本上正常工作,因此崩溃是代价:

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
    let cell = self.collectionView(collectionView, cellForItemAt: indexPath)
    let size = cell.systemLayoutSizeFitting(...)
    return CGSize(width: collectionView.frame.width, height: size.height)
}

我们正在加载带有 bundle 参数的 nib,nil如下所示。

let nib = UINib(nibName: "MyCell", bundle: nil)
collectionView.register(nib, forCellWithReuseIdentifier: "MyCell")

出于某种原因,这给了我们一个模糊的例外:

2018-11-02 10:15:01.744674-0400 Flights[385:17563] *** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[__NSArrayM objectAtIndexedSubscript:]: index 0 beyond bounds for empty array'
*** First throw call stack:
(0x18555ed8c 0x1847185ec 0x1854f7750 0x1854e48ac 0x18f266b04 0x18f266084 0x18f265fd8 0x18f265eb8 0x18f2859dc 0x18f286c00 0x18f286a2c 0x100b1c304 0x100ea8b6c 0x100eaa24c 0x100eaa30c 0x100eaa680 0x185566580 0x185445748 0x18544a56c 0x10573e0c8 0x1855642d4 0x18544a41c 0x18f266f60 0x18f266084 0x18f265fd8 0x18f265eb8 0x18f265500 0x18f264730 0x18f133770 0x1896d525c 0x1896d93ec 0x189645aa0 0x18966d5d0 0x18966e450 0x185506910 0x185504238 0x185504884 0x185424da8 0x187407020 0x18f40578c 0x100b8c760 0x184eb5fc0)
libc++abi.dylib: terminating with uncaught exception of type NSException

但是,像下面这样更改 bundle 参数.main(感谢评论)会给我们一个更合适的崩溃日志和调用堆栈。

let nib = UINib(nibName: "MyCell", bundle: .main)

这将我们引向问题的实际解决方案:不要collectionView(collectionView, cellForItemAt: indexPath)在布局期间调用。


推荐阅读