Ios 由于未捕获异常而终止应用程序';nsrange异常';调用dequeueReusableCellWithReuseIdentifier:forIndexPath:

Ios 由于未捕获异常而终止应用程序';nsrange异常';调用dequeueReusableCellWithReuseIdentifier:forIndexPath:,ios,objective-c,ios11,Ios,Objective C,Ios11,我们遇到了车祸,从车祸日志中可以看到: 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

我们遇到了车祸,从车祸日志中可以看到:

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设备上重现这一点。但我们所有的其他测试设备都工作正常


我们几乎陷入困境。以前有人碰到过这个问题,知道解决办法吗

不确定它是否是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)
}
我们正在加载绑定参数为
nil
的nib,如下所示

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)

您是否在
tableView
中为此
reuseIdentifier
注册了单元格类或nib?我们使用的是collection view,但是在此之前,我们使用了
registerClass:forCellWithReuseIdentifier:
。您确定这就是它崩溃的代码行吗?发布
cellForItemAt
功能的全部内容请显示。。。numberOfRowsInSection:代码和cellForRow的部分。。。访问模型(数组)的位置。此崩溃是由于访问空数组造成的。@mag_zbc和@danh感谢您的帮助。这确实是因为注册<代码>集合视图.寄存器(UINib(nibName:“MyCell”,bundle:.main),forCellWithReuseIdentifier:“MyCell”)有效。但出于某种原因,即使文档中说“如果指定nil,此方法将在主捆绑包中查找nib文件”,但将捆绑包设置为
nil
也不起作用。在文档中:sad_face:此问题仅发生在iOS 11上。我们也有类似的情况,但iOS 12和iOS 13不受影响。