UIImageView returning nil in a custom UICollectionViewCell

前端 未结 3 1404
面向向阳花
面向向阳花 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: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.

提交回复
热议问题