简体   繁体   中英

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:

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'

I have an EventCell.xib file and an EventCell.swift file with the EventCell class in it. 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. I can't figure out what iOS 13 has changed on UICollectionView , as the code compiles successfully, but it fails on runtime.

Here is where the cell is registered inside 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

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.

I had the exact same situation, changing contentInset before calling collectionView.register(...) in a ViewController, that was causing the crash.

I've deployed an update and will update the answer in a few weeks if crashes no longer happen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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