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

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

问题描述

上周我问了一个类似的问题,但我认为我已经将其范围缩小到更具体的问题所在.正在加载自定义单元格,并且当我使用 collectionView.dequeReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as 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!

}

我收到的错误是由于解开可选的 nil 值而导致的终止.问题肯定是 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!

编辑/解决

问题在于与 UICollectionView 的连接.我有它的参考,但我没有正确使用它.我试图在情节提要中设置数据源和委托,这对我来说似乎是个问题.当我转到代码并进行引用并以我对这两个调用的方式引用它时,它立即修复了它.如果遇到同样的问题,我希望这可以帮助其他人!现在开始自定义它,因为它弹出!

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 连接并通过代码设置委托和数据源而不是弄乱情节提要,它立即解决了问题.我用来设置委托和数据源的代码如下,仅取决于连接是否正确.

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天全站免登陆