3

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
}
  • > must register a nib or a class for the identifier or connect a prototype cell in a storyboard – Mojtaba Hosseini Sep 23 '19 at 20:43
  • 1
    Update your question with the complete method that registers the cell. And is this all done in code or is there a storyboard involved for the collection view and/or the cell? – rmaddy Sep 23 '19 at 21:05
  • I’d suggest adding a breakpoint or log statement where you register the NIB with that identifier to make sure it’s hitting that line. – Rob Sep 23 '19 at 21:15
  • I’d also suggest you start eliminating variables. E.g. try it on a 12.2 simulator and confirm whether the problem is the iOS version or something in your project. Also, you say it “keeps working on iOS 12.4 physical device” ... is this the same build that you’re trying on your device, or perhaps is it an old build? – Rob Sep 23 '19 at 21:25
  • Possible duplicate of [Error could not dequeue a view of kind UICollectionElementKindCell](https://stackoverflow.com/questions/15058975/error-could-not-dequeue-a-view-of-kind-uicollectionelementkindcell) – Adrian Sep 23 '19 at 21:25
  • @Adrian FWIW, I think Riccardo understands that cell reuse identifiers need to be registered (he tells us that it did call `register`), so I don’t think that’s a duplicate. The issue is more likely that he’s not calling it like he thinks it is or something like that. Unfortunately, he hasn’t provided enough to let us reproduce the problem and until he shares the registration code, it’s going to be hard for us to help him. – Rob Sep 23 '19 at 22:47
  • 1
    @Rob Yeah, you're right. I should have read more closely. Definitely need to see the registration code from viewDidLoad. – Adrian Sep 23 '19 at 23:00
  • I updated the topic with the code that registers the cell into the `collectionView`. I’ve tried again the code and it works on all the iOS simulators running iOS 12.2. I’m now trying to create a new project with Xcode 11 with the classes that are involved in the crash, maybe there are some configurations that are wrong or missing for iOS 13. – Riccardo Ruspoli Sep 24 '19 at 11:17
  • Unfortunately, starting a new project with a code that does the same as the original to exclude a configuration error in the project didn’t work. So, for now, I believe it’s some sort of problem with Xcode or the new SDK. I didn’t find documentation regarding changes in the `collectionView` that can affect my code. – Riccardo Ruspoli Sep 24 '19 at 19:31
  • 1
    Could you show your whole code in `viewDidLoad`? 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. – Balázs Vincze Nov 28 '19 at 11:28

1 Answers1

2

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.

Lord Zsolt
  • 6,492
  • 9
  • 46
  • 76