2016-10-29 8 views
0

ビューコントローラ内で2つのコレクションビューを使用しようとしていますが、このエラーメッセージが表示されます。あなたはこの問題を解決するために私が見逃していることを指摘できますか?事前にありがとうございます.. 以下は "OtherViewController"という名前のビューコントローラーの私のコードです。UICollectionViewは、nil以外のレイアウトパラメータで初期化する必要があります。

クラスOtherViewController:のUIViewController、UICollectionViewDataSource、UICollectionViewDelegate {

//1. CREATE A VARIABLE FOR YELLOW ARRAY SO THAT I CAN DEFINE A STRING 
var exploreArray = [String]() 
var exploreHeader = [String]() 
var yellowImages = [String]() 
var explorecategories = [String]() 


@IBOutlet weak var mycollectionView: UICollectionView! 


var collectionViewA = UICollectionView() 
let collectionViewB = UICollectionView() 
let collectionViewAIdentifier = "yellowCell" 
let collectionViewBIdentifier = "yellowCell2" 

override func viewDidLoad() { 
    super.viewDidLoad() 


    // Do any additional setup after loading the view. 

     collectionViewA.delegate = self 
     collectionViewB.delegate = self 

     collectionViewA.dataSource = self 
     collectionViewB.dataSource = self 


     self.view.addSubview(collectionViewA) 
     self.view.addSubview(collectionViewB) 

}

FUNCのcollectionView(_ collectionView:UICollectionView、numberOfItemsInSection部:INT) - >のInt {

if collectionView == self.collectionViewA { 
     return exploreArray.count  } 

    return 1 // Replace with count of your data for collectionViewB 

} 
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { 
    if collectionView == self.collectionViewA{ 

     let cellA = collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell", for: indexPath) as! yellowCell 
       // Set up cell 

    //DISPLAY TITLE LABEL 
    cellA.yLabel.text = exploreHeader[indexPath.row] 

    //DISPLAY IMAGES 
    cellA.yellowImages.image = UIImage(named:yellowImages [indexPath.row]) 

    //DISPLAY DESCRIPTION 
    cellA.yellowTextField.text = exploreArray [indexPath.row] 

    return cellA 
    } 

    else { 

     let cellB = collectionView.dequeueReusableCell(withReuseIdentifier: "yellowCell2", for: indexPath) as! yellowCell2 

    cellB.labe2.text = "Dolphin" 

// cellBがコレクションビューに表示されるかどうかを見るにはradomのラベル

   return cellB 
    } 

    } 

答えて

0

私がthis questionに行った回答は、あなたの状況にも役立つと思います。あなたが設定と一緒に、それは(人々Iよりもはるかに賢く!のカップルが)推奨されていた

var collectionViewA = UICollectionView() 
let collectionViewB = UICollectionView() 

ではなく、のviewDidLoad()で次に

var collectionViewA: UICollectionView! 
var collectionViewB: UICollectionView! 

を使用する必要があります。具体的

、デリゲートとデータソースが(あなたがそうであるように)、最終的にはAFTEその後、

let layoutA = UICollectionViewFlowLayout()     
layoutA.itemSize = CGSize(width: 100, height: 100) 

let layoutB = UICollectionViewFlowLayout() 
layoutB.itemSize = CGSize(width: 100, height: 100) 

collectionViewA = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutA) 
collectionViewB = UICollectionView(frame: self.view.frame, collectionViewLayout: layoutB) 

のようなものを追加し、 rサブビューを追加する、

collectionViewLeft.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewLeftIdentifier) 
collectionViewRight.register(UICollectionViewCell.self, forCellWithReuseIdentifier: collectionViewRightIdentifier) 

完全なコードは、リンクされた質問の回答に表示されます。 (それはA &Bの代わりに&を使っていますが、同様の考え方がうまくいきます)

関連する問題