Reputation: 1013
I call firebase database to retrieve names of locations. I store the data in an array of dictionaries, then populate collectionView cells with the data from the array. when the collection view cell is selected, I segue to another view controller and pass the data to a collectionViewCell inside the view controller.
The console prints the correct values, but when I set a UILabel text equal to the value, the compiler throw a nil optional unwrap error.
Passing the data to the view controller in didSelectItemAt:
Setting a value to a UILabel text:
Am I passing the data incorrectly? Im not sure why the console is printing the value, but compiler throws an error. Any insight as to why this happens, and how to correct this is very appreciated.
EDIT: ozzieozumo was correct, I was incorrectly instantiating my cell class. I was creating a new instance of my cell, but had no connection to storyboard, which led to my label being nil.
SOLUTION: I edited my segue method to take my dictionary as a parameter. instead of instantiating the CollectionViewCell in the method, I instantiated the CollectionViewController and passed my dictionary to the CollectionViewController. In the Controller under cellForItemAt, I set pass the data to the cell.
Upvotes: 0
Views: 67
Reputation: 436
The assignment statement fails because the left hand side is nil, not the right hand side.
Your outlet is nil because of the way you are instantiating the cell instance.
let detailImageCell = LocationDetailImagesCell()
That will create a new instance of your cell but that instance has nothing to do with your storyboards/xibs, and so the outlets will be uninitialized.
That is your immediate problem here.
Upvotes: 1