2017-08-24 6 views
-1

これを熟読してください。私は2つのコレクションビューを実装しているという見解があります。 2つのコレクションビューはほぼ同じように設定されています。2つの異なるコレクションビューを使用する場合の再利用識別子

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier CollectionViewPantsCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard' 

私はそれ私のCollectionViewShirtCellとまったく同じ登録して、意味がありません。私は私の見解に両方を追加すると

は、私のアプリは、エラーを呼び出します。

デバッグするために、私はパンツコレクションを削除し、私の "cellForItemAt"メソッドを本当にシンプルに書き直しました。 1つのcollectionViewでうまくいきました。

違いは何ですか?私はコードの中にいくつかのノートを追加しました。

import UIKit 

class HomeController: UIViewController, UICollectionViewDelegateFlowLayout, UICollectionViewDataSource { 

    let collectionViewShirtsIdentifier = "CollectionViewShirtsCell" 
    let collectionViewPantsIdentifier = "CollectionViewPantsCell" 

    var shirtStore: ShirtStore! 
    var pantStore: PantStore! 


    public var collectionViewShirts : UICollectionView{ 

     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = UICollectionViewScrollDirection.horizontal 

     let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2), collectionViewLayout: layout) 
     collectionView.backgroundColor = UIColor.orange 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewShirtsIdentifier) 

     return collectionView 
    } 

    public var collectionViewPants : UICollectionView{ 

     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = UICollectionViewScrollDirection.horizontal 

     let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2), collectionViewLayout: layout) 
     collectionView.backgroundColor = UIColor.blue 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewPantsIdentifier) 

     return collectionView 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     navigationItem.title = "Hanger" 
     self.view.addSubview(collectionViewShirts) 
     ///... CANT ADD THE SECOND COLLECTION???? 
     // self.view.addSubview(collectionViewPants) 
    } 

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
     if collectionView == self.collectionViewShirts { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionViewShirtsIdentifier, for: indexPath as IndexPath) 

      return cell 
     } 
     else 
     { 
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: collectionViewPantsIdentifier, for: indexPath as IndexPath) 

      return cell 
     } 
    } 

    ///... THIS VERSION WORKS IF I ONLY TRY TO MANIPULATE ONE COLLECTION 
    // func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    //  let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CollectionViewShirtsCell", for: indexPath as IndexPath) 
    //   
    //  return cell 
    // } 

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{ 
     if(collectionView == collectionViewShirts) 
     { 
      print(shirtStore.allShirts.count) 
      return shirtStore.allShirts.count 
     } 
     else if (collectionView == collectionViewPants) 
     { 

      return pantStore.allPants.count 
     } 
     else 
     { 
      return 5//shoeStore.allShoes.count 
     } 
    } 
} 
+0

私は 'collectionViewShirtsIdentifier' –

+0

あなたの問題は、この行にある' collectionView.register(UICollectionViewCell.self、forCellWithReuseIdentifier:collectionViewPantsIdentifier)のための1つのレジスタのみメソッド見ることができます 'あなたが誤ってあなたのセルを登録している –

+0

@ReinierMelianは、なぜそれがシャツのために働くだろうが?彼らは同じではありませんか? – user1093111

答えて

3

あなたcollectionViewShirtscollectionViewPants変数を設定する方法が間違っています。その結果、cellForItemAt:ifテストではelseになり、 'シャツ'コレクションビューの 'パンツ'セルをデキューしようとしています。あなたが適切にあなたのコレクションビューを宣言する必要が

public var collectionViewShirts : UICollectionView = { 

     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = UICollectionViewScrollDirection.horizontal 

     let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2), collectionViewLayout: layout) 
     collectionView.backgroundColor = UIColor.orange 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewShirtsIdentifier) 

     return collectionView 
    }() 

    public var collectionViewPants : UICollectionView = { 

     let layout = UICollectionViewFlowLayout() 
     layout.scrollDirection = UICollectionViewScrollDirection.horizontal 

     let collectionView = UICollectionView(frame: CGRect(x: 0, y: 0, width: view.frame.width, height: view.frame.height/2), collectionViewLayout: layout) 
     collectionView.backgroundColor = UIColor.blue 
     collectionView.delegate = self 
     collectionView.dataSource = self 
     collectionView.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewPantsIdentifier) 

     return collectionView 
    }() 

は、閉鎖の最後に=()に注意してください。これにより、Swiftは匿名関数を呼び出して返された値を割り当てます。

フレームを直接設定するのではなく、制約を使用することをお勧めします。これはビューの回転では機能せず、コレクションビューを初期化するときにフレームが正しく設定されないためですフレームは、viewDidLayoutSubviewsが呼び出されたときのみ設定されます。

前述したように、ストーリーボードでこのようなことを行うのはずっと簡単です。

+0

ありがとう、もう一度。インスタンスを作成するのではなく、viewDidLoad()内のすべてをビルドすることになりました。 – user1093111

関連する問題