2016-10-29 4 views
0

보기 컨트롤러에서 두 개의 컬렉션보기를 사용하려고하는데 결과적으로이 오류 메시지가 나타납니다? 이 문제를 해결하기 위해 제가 누락 된 부분을 지적 해 주시겠습니까? 미리 감사드립니다. 아래보기 컨트롤러에서 "OtherViewController"라는 코드가 있습니다.UICollectionView가 non-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) -> 지능 {

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! 

를 사용해야 특히

, 대리인 및 데이터 소스 (사용자가 수행하는 것처럼)를 추가하십시오.

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) 

그런 다음 마지막으로 afte r 하위 보고서 추가,

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

전체 코드는 연결된 질문에 대한 답변으로 표시됩니다. (& B 대신 왼쪽 &을 사용하지만, 같은 아이디어가 적용될 수 있습니다.)