2016-04-29 30 views
4

我正在製作一個集合視圖,並且創建了自己的自定義collectionCell。我在身份檢查員中已經說明了這一點,並且據我所知,我做的一切都是正確的。爲什麼我不能向下轉發UICollectionViewCell到它的子類?

的代碼是

import UIKit 

class SubjectCollectionViewCell: UICollectionViewCell { 
    @IBOutlet weak var imageView: UIImageView! 
    @IBOutlet weak var label: UILabel! 
} 

,並在我的collectionViewController

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

    let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SubjectCollectionViewCell 

    return cell 
} 

我收到以下錯誤消息

「無法投類型的值 'UICollectionViewCell'(0x110460820)爲' project.SubjectCollectionViewCell'(0x10eceeda0)。 (lldb)「

+4

你確定這是自動的Xcode在viewDidLoad中(產生的線正在出隊正確的細胞?確保重用標識符正確,並且還要在界面構建器中設置自定義單元類。 – almas

回答

-2

您是否註冊了reuseIdentifier以便它將創建正確類的對象?

+2

這是一個問題,而不是答案。應作爲評論發佈。 – rmaddy

1

發生什麼事是,在viewDidLoad中()時,Xcode自動genere這樣一行來定義單元格:

self.collectionView .register(UICollectionViewCell.self,forCellWithReuseIdentifier:reuseIdentifier)

而且你正在創建自己的方法來做到這一點,所以,你需要刪除)

相關問題