2016-10-29 3 views
0

Я пытаюсь использовать два представления коллекции внутри контроллера представления, и я получаю это сообщение об ошибке в результате? Можете ли вы указать, что мне не хватает, чтобы исправить эту проблему? Спасибо заранее. Ниже мой код в контроллере представления с именем «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) 

}

функ 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" 

// just radom, чтобы увидеть, появится ли CellB в виде коллекции

   return cellB 
    } 

    } 

ответ

0

Я думаю, что ответы, которые я получил до this question, могут быть полезны и для вашей ситуации.

В частности, если у вас есть

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

рекомендовалось (на пару людей гораздо умнее, чем я!), Чтобы использовать вместо

var collectionViewA: UICollectionView! 
var collectionViewB: UICollectionView! 

Тогда в viewDidLoad(), наряду с установкой делегат и источник данных (как и вы), добавьте что-то вроде

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, но те же идеи, надеюсь, применимы.)

Смежные вопросы

 Смежные вопросы