2017-01-24 12 views
2

xibから作成したカスタムセルがあります。カスタムCollectionViewセルをサブクラス化すると親UIが表示されない

それは次のようになります。

collectionViewCell

、私はそれをサブクラスのみクラスで作成した2個の他の細胞を持っています。

ので、私UIViewControllerに私はすべてのセルを登録containerView

に異なるビューを追加する必要があります。

@IBOutlet weak var collectionView: UICollectionView! { 
    didSet { 
     collectionView.dataSource = self 
     collectionView.delegate = self 
     collectionView.register(cell1.self, forCellWithReuseIdentifier: "cell1") 
     collectionView.register(cell2.self, forCellWithReuseIdentifier: "cell2") 
     collectionView.register(UINib(nibName: "ParentCell", bundle: nil), forCellWithReuseIdentifier: "parentCell") 
    } 
} 

このセルをデキューします。

fatal error: unexpectedly found nil while unwrapping an Optional value

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

    if indexPath.row == 0 { 
     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! cell1 
     cell.containerView.backgroundColor = UIColor.yellow 
     return cell 
    } 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! cell2 
    cell.containerView.backgroundColor = UIColor.red 

    return cell 
} 

が、アプリがクラッシュ私はこれを実装する方法containerView

任意の提案を使用しようとすると?
おかげ


EDIT:

私の問題は、親細胞からの景色がインスタンス化取得されていないということです。 私は何を理解する

fatal error: unexpectedly found nil while unwrapping an Optional value

を意味します。ビューがインスタンス化されない理由は分かりません。

クラッシュが

cell.containerView.backgroundColor 
+1

[「致命的なエラー:予期せずに任意の値アンラップしながら、nilを見つけ、」何?意味を]の可能複製(http://stackoverflow.com/questions/32170456/what-致命的なエラー - 予期せぬことに - 見つからない - オプションの値なし) –

+0

@ Mr.Bistaもう一度同じ質問を読んでください。 – ilan

+0

私は知っていますが、エラーは自明ですが、エラーが発生した行を正確に指摘できますか? –

答えて

-2

あなたがすることによってゼロvaribleを使用するために、このエラーにあります!

チェック2行

let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) as! cell1 

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath) as! cell2 
+0

何ですか?理解できませんでした。 – ilan

+0

セルのこのエラーはnilです。 – mohsen

関連する問題