Can multiple view controllers use a single uicollectionviewcell? = Yes, but

I am (slowly) progressing on building a cookbook app for my mother’s recipes.

I have built a ContentsViewController, in which I have a UICollectionView that displays a collection of Chapters in the body of the screen. The ChapterCell has a chapterImage and a chapterName.

When a user taps a Chapter, the screen segues to the ChapterViewController. The collection of Chapters then displays in the left-hand column, and a table of recipes will appear in the body of the screen.

My intention is to re-use the ChapterCell in that left-hand column, but when I do, I get the following app crash in AppDelegate
unexpectedly found nil while unwrapping an optional value uicollectionviewcell
I have put a breakpoint in and done various versions of ‘po chapter’ through to ‘po chapterToDisplay!chapterName’ and values are returned.

As a workaround to keep me going, I have created an almost identical ChapterCell and called it ChapChapterCell. BUT this is going to gnaw away at me, and would like to know one way or the other.

Having googled around, I can see stuff about using multiple collection view in a view controller, but nothing much about what I am trying to do.

… So can multiple view controllers use a single uicollectionviewcell?

I know it is possible, as I have looked at a different app I built and it shares a common ‘uitableviewcell’. And in the cookbook app I am building I am getting the same error for the ‘uitableviewcell’ as the ‘uicollectionviewcell’. This is despite following the same code patern from the previous app. Something else is going wrong, as I am able to run the other app in Xcode 11.3 simulator / devices just fine. Just not the new one. :exploding_head:
The difference between the two apps, is the original app only has an ‘AppDelegate.swift’ file, whereas the new one has both an ‘AppDelegate.swift’ file, as well as a ‘SceneDelegate.swift’ file. I am deducing that the problem has got to be in there somewhere.

The error thrown is

***Assertion failure in -[UITableView _dequeueReusableCellWithIdentifier:forIndexPath:usingPresentationValues:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKitCore_Sim/UIKit-3901.4.2/UITableView.m:8624
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'unable to dequeue a cell with identifier TableOfRecipesCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'

The app works on the first View Controller, but when I tap a row in the second View Controller it crashes the app.
As mentioned, I have double- through quintuple-checked that the second VC and Cell have the cell identifier is correctly set up. It is the same problem for both the Collection and Table views.

I have also tried copying over the ‘AppDelegate’ file from the original app to the new app, but this does not fix it. :frowning:

My conclusion is that a different code pattern is needed to handle reusing Cell views across View Controllers when both an ‘AppDelegate.swift’ file, as well as a ‘SceneDelegate.swift’ file is automatically included in a new app. OR it is no longer possible to reuse Cell views across View Controllers - but that would be daft!!

Hey @lachlan. Feel free to zip up your project, and send it over to me, and I will take a look. You can send me the version without the workaround, and I will do my best to debug.

Please upload the file to a file sharing service like Box, and paste the link in here. Also, make sure to zip up the entire project folder, not just the Xcode file.

Hi David, thanks for replying and offering to help. I have the app up in GitHub. I can add you as a collaborator (assuming you have an account name you could send me)

Yep, my GitHub is druvinskiy.

Oh, you will see another collaborator there too, who is an iOS developer my employer uses as a contractor, but I normally prefer not to bother her (she is also on holidays at the moment) … but I was at my wits-end!

Okay. I was able to download the project. What are the exact steps I need to take to get the app to crash as you described?

Also, I have left the CollectionView workaround in there ‘ChapChapterCell’ as a duplicate of ‘ChapterCell’. But the TableOfRecipeCell problem is still there. And I am assuming if a solution is found for TableView, then it would likely be the same for Collection View

Steps to Reproduce:

  1. tap DESSERTS
  2. Tap any of the Recipe Names in the segued New Controller
    app crash

Okay, this is the error I see.

Fatal error: Index out of range
2020-01-01 19:17:43.832264-0600 CookAlpha[19062:1323917] Fatal error: Index out of range

This is a different error than the one you posted above. Did you need help with both?

I am not getting that error

… hang on, I was getting that error if I set selectedChapterIndex = 0, so I temporarily set it to 1. I was going to get back to fixing that later

Hmm. Did you want me to look at that error?

If Index out of range is crashing the app for you, then yes

Okay, I started looking at your code. It seems like the array that you are getting back from cookbook.chapters[selectedChapterIndex].recipes in cookbookRetrieved is empty, causing your recipes array to be empty. Is this what you intended?

No it is not intended. The same code paternities was used in ChapterViewController, and it works there. But it is empty in RecipeVC :frowning: it should be populated.

As mentioned above, I have put a breakpoint in at that point and done various versions of ‘po chapter’ through to ‘po chapterToDisplay!chapterName’ and values are returned.
The crash happens after that point in AppDelegate

I think that the array is (somehow) emptied when it hits AppDelegate and that causes the app crash

Apologies. I did the breakpoint in Collection, not Table (which I have just done), and yes the array is empty at this point.

I have just fixed that as I was referencing selectedChapterIndex. I will push another build of the app, as the same problem is still happening. Putting a breakpoint there now has po recipes returning