2016-12-04 87 views
0

我正在设置一个显示图像的UICollectionView的新项目。我创建了ViewControllerCellViewController,就像它应该的那样。'UICollectionViewCell'类型的值没有成员'imageView'

CellViewController的代码如下:

import UIKit 

class ClubCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var imageView: UIImageView! 
} 

但在ViewController当我设置它给我的错误形象。下面的代码:

override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 


    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, 
                 for: indexPath) 
    // Configure the cell 
    cell.backgroundColor = UIColor.black 
    cell.imageView.image = UIImage(named: "pic1") 
    return cell; 
} 

在此先感谢..

+0

你得到的错误是什么?你把你的'@ IBOutlet'连接到你的'.xib'文件了吗? – dirtydanee

+0

我得到的错误是'UICollectionViewCell'类型的值没有成员'imageView'尽管我已将单元格连接到我的故事板上的新类ClubCollectionViewCell! –

回答

1

你应该投你collectionViewCell子类作为您的自定义一个ClubCollectionViewCell

使用guard来检查单元格的类型,如果它失败,请执行fatalError()

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 

    guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, 
                  for: indexPath) as? ClubCollectionViewCell else { 
                    fatalError("Wrong cell class dequeued") 
    } 
     // Configure the cell 
     cell.backgroundColor = UIColor.black 
     cell.imageView.image = UIImage(named: "pic1") 
     return cell 
} 
+0

谢谢你的回答,但我试图做到这一点,应用程序崩溃! –

+0

你使用正确的'reuseIdentifier'吗? – dirtydanee

+0

是的。我已经检查过 –

相关问题