简体   繁体   English

为什么将 UICollectionView 上的单元格出列会导致应用程序在 iOS 13 上崩溃?

[英]Why dequeuing a cell on UICollectionView causes the app to crash on iOS 13?

I updated today to Xcode 11 from 10.3, and I started my app on an iPhone simulator running iOS 13. Immediately the app crashed giving the following error:我今天从 10.3 更新到 Xcode 11,我在运行 iOS 13 的 iPhone 模拟器上启动了我的应用程序。应用程序立即崩溃,出现以下错误:

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier EventCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无法使一种视图出列:带有标识符 EventCell 的 UICollectionElementKindCell - 必须为标识符注册一个 nib 或 class 或连接故事板中的原型单元格”

I have an EventCell.xib file and an EventCell.swift file with the EventCell class in it.我有一个EventCell.xib文件和一个EventCell.swift文件,其中EventCell class。 Now, I already called collectionView.register() , and registered my Nib in viewDidLoad() , in fact this code keeps working on my iOS 12.4 physical device and on iOS 12.2 simulators.现在,我已经调用了collectionView.register() ,并在viewDidLoad()中注册了我的 Nib,事实上,这段代码一直在我的 iOS 12.4 物理设备和 iOS 12.2 模拟器上工作。 I can't figure out what iOS 13 has changed on UICollectionView , as the code compiles successfully, but it fails on runtime.我无法弄清楚 iOS 13 在UICollectionView上发生了什么变化,因为代码编译成功,但它在运行时失败。

Here is where the cell is registered inside viewDidLoad :这是单元格在viewDidLoad中注册的位置:

// MARK: UICollectionView
collectionView.register(UINib(nibName: "EventCell", bundle: nil), forCellWithReuseIdentifier: "EventCell")
collectionView.contentInset = UIEdgeInsets(top: 65, left: 0, bottom: 0, right: 0)
collectionView.scrollIndicatorInsets = UIEdgeInsets(top: 65, left: 0, bottom: 0, right: 0)
collectionView.backgroundColor = applicationScheme.colorScheme.backgroundColor

This the code that creates the cell:这是创建单元格的代码:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EventCell", for: indexPath) as! EventCell

    cell.imageView.image = UIImage(named: "fill")
    cell.titleLabel.text = "Titolo di prova"
    cell.dateLabel.text = "00/00/0000"
    cell.timeLabel.text = "00:00"

    return cell
}

One possible answer is the last comment from dequeueReusableCell Balázs Vincze一个可能的答案是 dequeueReusableCell Balázs Vincze的最后一条评论

I had the exact same issue, but I was registering the cell after setting the contentInset, and on iOS 13, changing the contentInset triggered a layout update on the collection view, which called cellForItemAt, before the cell was actually registered.我遇到了完全相同的问题,但我在设置 contentInset 后注册了单元格,在 iOS 13 上,更改 contentInset 触发了集合视图上的布局更新,称为 cellForItemAt,在单元格实际注册之前。

I had the exact same situation, changing contentInset before calling collectionView.register(...) in a ViewController, that was causing the crash.我遇到了完全相同的情况,在 ViewController 中调用collectionView.register(...)之前更改contentInset ,这导致了崩溃。

I've deployed an update and will update the answer in a few weeks if crashes no longer happen.我已经部署了更新,如果不再发生崩溃,我会在几周内更新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM