UIImageView在自定义UICollectionViewCell中返回nil [英] UIImageView returning nil in a custom UICollectionViewCell

查看:110
本文介绍了UIImageView在自定义UICollectionViewCell中返回nil的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上周我问了一个类似的问题,但我想我已经把它缩小到更具体的问题。当我使用 collectionView.dequeReusableCellWithIdentifier(MenuCell,forIndexPath:indexPath)作为MenuCollectionViewCell MenuCell是重用标识符时,正在加载自定义单元并且看起来正确调用对于我的自定义单元格和MenuCollectionViewCell是保存自定义代码的swift文件。从调试代码我已经确定没有加载UIImageView。我做了一张支票,这就是我想出来的。为什么没有加载ImageView并且只显示为nil我不知道。我将发布加载文件的代码和自定义单元格代码。

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!

}

我收到的错误是由于展开而终止可选的零值。问题是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

EDIT/SOLVED

问题是与UICollectionView的连接。我有参考它,但我没有正确使用它。我试图在故事板中设置dataSource和委托,这对我来说似乎是个问题。当我移动到代码并制作引用并以我对这两个调用所做的方式引用它时,它立即修复它。我希望如果他们遇到同样的问题,这可以帮助别人!现在开始自定义它,因为它弹出来了!

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!

推荐答案

问题根本不在于UIImageView。 Rdelmar在检查出口是否正确的声明中是正确的。 menuCollectionView 的出口参考未正确链接,这导致错误弹出。通过修复 @IBOutlet 连接并通过代码设置委托和dataSource而不是弄乱故事板,它立即解决了问题。我用来设置委托和dataSource的代码如下,并且仅取决于连接是否是正确的连接。

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!

这篇关于UIImageView在自定义UICollectionViewCell中返回nil的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆