UIImageView returning nil in a custom UICollection

2019-03-03 03:25发布

问题:

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 correctly when I use the collectionView.dequeReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as MenuCollectionViewCell "MenuCell" is the reuse identifier for my custom cell and MenuCollectionViewCell is the swift file that holds the custom code. From debugging the code I have determined that the UIImageView is not being loaded. I did a check and that is what I came up with. Why the ImageView is not being loaded and is only appearing as nil I do not know. I will post the code for the file where it is being loaded and the custom cell code.

class MenuViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate
{
    // Not sure if I really need this
    @IBOutlet var menuCollectionView: MenuCollectionView!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        // Do any additional setup after loading the view
        view.backgroundColor = UIColor(patternImage: UIImage(contentsOfFile: "behind_alert_view.png"))
        // This was the fix here
        self.menuCollectionView.delegate = self
        self.menuCollectionView.dataSource = self

    }

    // Variables
    var menuImagesArray = ["MyProfileIcon.png"]

    // Data Source Protocol
   func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int
    {
        return 1
    }

    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
    {
        return menuImagesArray.count
    }

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
    {
        var menuCell: MenuCollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("MenuCell", forIndexPath: indexPath) as MenuCollectionViewCell

        var image: UIImage!

        if ((menuCell.imageView) != nil)
        {
            image = UIImage(named: menuImagesArray[indexPath.row])
        }

        menuCell.imageView.image = image



        return menuCell
    }

}

And here is the custom cell file:

class MenuCollectionViewCell: UICollectionViewCell
{
    @IBOutlet var imageView: UIImageView!

}

The error that I am receiving is a termination due to unwrapping a nil value for an optional. The problem is the UIImageView for sure, any help figuring this out would be great!

EDIT/SOLVED

The problem was the connection to the UICollectionView. I had the reference to it but I had not used it properly. I was trying to set the dataSource and delegate in the storyboard and that seemed to be the problem for me. When I moved over to the code and made the reference and referenced it in the way I did with those two calls it instantly fixed it. I hope this can help someone else if they run into the same problem! Now on to customizing it since it pops up!

回答1:

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.



回答2:

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!



回答3:

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