UIImageView在自定义的UICollectionViewCell中返回nil

我上周也提过一个类似的问题,但是我想我已经把它缩小到更具体的问题了。 自定义单元格正在加载,并且当我使用collectionView.dequeReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as MenuCollectionViewCell “MenuCell”是我的自定义单元格的重用标识符和MenuCollectionViewCell是swift文件,它保存的swift文件,当我使用collectionView.dequeReusableCellWithIdentifier("MenuCell", forIndexPath: indexPath) as MenuCollectionViewCell自定义代码。 从debugging代码,我已经确定UIImageView没有被加载。 我做了一张支票,这就是我想出来的。 为什么ImageView没有被加载,只是出现为零,我不知道。 我将发布它正在加载的文件和自定义单元代码的代码。

 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 } } 

这里是自定义单元格文件:

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

我收到的错误是由于展开可选的零值而终止。 问题是UIImageView肯定的,任何帮助搞清楚这将是伟大的!

编辑/解决

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

像上面那样连接UICollectionView不是必须的,因为已经完成了,但是分配委托和数据源是。 这可以通过这种方式完成:

 self.collectionView?.delegate = self self.collectionView?.dataSource = self 

我发现这个答案,真的很兴奋,因为我有这个问题。 但是,在检查我的连接并确保代理和数据源被正确分配后,我的imageView仍然是零。

对我来说,我已经注册了我的新的子类在故事板,所以我不得不从我的代码中删除这个调用:

 self.myCollectionView.registerClass(MyCollectionViewCell.self, forCellWithReuseIdentifier: Constants.reuseIdentifier) 

这是取代故事板中的注册,导致imageView为零。 我删除了代码后,事情奏效。 谢谢你的问题SamG,并希望这可以帮助其他谁可能有这两个问题之一。

这个问题根本不在UIImageView中。 Rdelmar在检查网点是否正确的陈述中是正确的。 menuCollectionView的插口引用没有正确链接,这导致错误popup。 通过修复@IBOutlet连接并通过代码设置委托和数据源,而不是与故事板混淆,它立即清除了问题。 我用来设置委托和数据源的代码如下,完全取决于连接是一个适当的连接。

 self.menuCollectionView.delegate = self self.menuCollectionView.dataSource = self 

简单的修复。 我希望这可以帮助别人,如果他们遇到类似的问题! 先检查连接! 然后通过并确保一切都在做它应该做的!

对我而言,这个经常讨论的话题没有任 最后我解决了这个问题,把“Collection Reusable View”标识添加到故事板定制单元格中。 这个值被设置为与DataSource-Function dequeueReusableCell(withReuseIdentifier:…)中相同的值。 我认为这是一个非常基本的事情:)