2017-02-05 12 views
0

です。これはImageController(ViewController)のクラスです コレクションビューの背景は表示されません。どんな助け?私が正しく初期化していないものはありますか?ここでUICollectionViewの背景が表示されていない間は、UICollectionViewのセルは

クラスです:

class ImageController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate { 

    let reuseIdentifier = "imageCell" 

    @IBOutlet weak var collectionView: UICollectionView! 

    override func viewDidLoad() { 
     super.viewDidLoad() 
    } 

    required init?(coder aDecoder: NSCoder) { 
     super.init(coder: aDecoder) 
    } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     return 10 
    } 

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

     let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath as IndexPath) as! ImageCell 

     cell.myImage.image = unknown //name of image, isn't the cause of error 

     return cell 
    } 

} 
+0

実際にデリゲートとデータソースを割り当てましたか?私はVCが両方に準拠するように設定されていると思うが、 'collectionView.delegate = self'と' collectionView.dataSource = self'といっしょにしなければならない。 – Pierce

答えて

0

あなたはviewDidLoad()に追加する必要があります。また

collectionView.delegate = self 
collectionView.dataSource = self 

を、(ブレークポイントを追加し、彼らが到達可能であるかどうかをチェック)メソッドをデバッグしてみてください。

これが役に立った。

+0

これはうまくいった!ただ簡単な質問:私はこれらの2行のコードを使用しない同様のクラス/コントローラを持っていますが、それでも問題なく動作します。それがなぜ起こっているのでしょうか? – Shekar

+0

おそらく、UICollectionViewControllerまたはUICollectionViewのデリゲートをViewController Sceneに追加しました。 – Aragunz

+0

@Shekarはい、ストーリーボードからcollectionViewを確認して、そのデリゲート/データソースが接続されているかどうかを確認したり、stoyrboardから自動的に接続されるUICollectionViewController –

0

上記のコード行を使用したくない場合は、collectionViewを右クリックし、ViewController Sceneに黄色の領域をドラッグし、delegateとdataSourceを選択します。 enter image description here

関連する問題