2016-12-04 86 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

是的。我已經檢查過 –

相關問題