I asked a similar question last week but I think I have it narrowed down to more specifically what is going wrong. The custom cell is being loaded and looks to be called correct
The problem was not at all with the UIImageView. Rdelmar was correct in his statement of checking that the outlets were correct. The outlet reference to menuCollectionView
was not linked properly and this was causing the error to pop up. By fixing the @IBOutlet
connection and setting the delegate and dataSource via code rather than messing with the Storyboard, it cleared up the problem immediately. The code I used to set the delegate and dataSource are as follows and solely depend on the connection being a proper connection.
self.menuCollectionView.delegate = self
self.menuCollectionView.dataSource = self
Simple fix really. I hope this can help someone else if they run into a similar problem! Check the connections first!!! Then go through and make sure everything is doing what it should!
For me no solution of this often discussed topic helped directly. Finally I solved it with adding the "Collection Reusable View"-identifier to the storyboard custom cell. This value I set to the same value as in the DataSource-Function dequeueReusableCell(withReuseIdentifier:...). I think this is a very basic thing :)
Connecting the UICollectionView as you do at the top is not necessary as that is already done, but assigning the delegate and datasource are. This can be done in this way:
self.collectionView?.delegate = self
self.collectionView?.dataSource = self
I found this answer and was really excited because I had exactly this problem. However, after checking my connectivity and ensuring the delegate and datasource were correctly assigned, my imageView was still nil.
It turned out for me that I was already registering my new subclass in the storyboard so I had to remove this call from my code:
self.myCollectionView.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: Constants.reuseIdentifier)
This was replacing the registration in the storyboard and resulting in imageView being nil for me. After I removed the code, things worked. Thanks for your question SamG and hope this helps others who might have one of these two issues.