UIImageView returning nil in a custom UICollectionViewCell

前端 未结 3 1353
面向向阳花
面向向阳花 2021-01-22 19:27

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

相关标签:
3条回答
  • 2021-01-22 19:45

    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!

    0 讨论(0)
  • 2021-01-22 19:51

    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 :)

    0 讨论(0)
  • 2021-01-22 19:54

    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.

    0 讨论(0)
提交回复
热议问题