There is a screen (CollectionViewController): enter image description here

Also I have a class where there are settings for the cell. enter image description here

I tied this class to a cell, assigned an identifier = "cell" to the cell. And when I try to tell Xcode that this cell has an ImageBoard, an error occurs when the program is running. enter image description here Without .. as! CellPhoto .. as! CellPhoto works fine, but I need to work with the cell. I would be very grateful if you tell me where I was wrong.

PS As far as I understand, because of the exclamation mark, an error occurs, because Cell cell is nil!

enter image description here

Pss

The issue is resolved, it was necessary to completely remove the register method! Thanks for the help :)

  • Better to add code with text and not a picture - 0rt
  • The last screenshot does not show a complete description of the error - 0rt
  • If you use a storyboard for a cell, why are you registering it? - Vitaly

2 answers 2

Look at the debugger before it crashes, whether you have a cell or its Outlets.

Most likely you incorrectly connected the Outlet and you have it nil (I mean imageBoard)

Also check if you specified your custom class at the cell in the storyboard and whether it is set correctly in the storyboard in the reuseIdentifier cell.

I want to remind you that if you create a cell directly in the storyboard, then you should not write collectionView.register ....... The system does everything for you. Try to delete it.

enter image description here

  • I reconnected, it still does not work. Created empty projects, and did there, too, the error takes off. It flies because of nil values, if I take a cell.label? .Text = "1", then because of? everything works, but there is no result. - LoonyMan
  • @LoonyMan updated the answer - Evgeny Lyozov 8:36
  • As far as I understand, for some reason, I think that the Cell does not exist. I returned it back to the register method, rechecked all the names of the connected classes to the objects, and reuseidentifier. Updated the question. - LoonyMan pm

In your case, the error occurs because the cell that you receive from collectionView is the usual UICollectionViewCell and you cast it as CellPhoto .

This is due to the fact that you have registered a regular UICollectionViewCell for reuseIdentifier

To fix this, register CellPhoto.self in the register(_ cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String) method register(_ cellClass: AnyClass?, forCellWithReuseIdentifier identifier: String)

  • Like this? self.collectionView! .register (CellPhoto.self, forCellWithReuseIdentifier: reuseIdentifier) - LoonyMan
  • Yes, in this case you will register CellPhoto and you can get it when using reuseIdentifier - 0rt
  • Now the error occurs with any interaction with the elements in this cell, updated the question - LoonyMan