2017-11-10 5 views
-1

collectionView에 등록 했음에도 불구하고 두 번째 셀을 표시 할 수 없습니다. 누구나 이것이 왜 그런지 알 수 있습니까? 다른 셀이 대신 표시하는 행을 전환 할 때 셀이 존재합니다.Swift로 여러 UICollectionViewCell을 표시 할 수 없습니다.

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     collectionView?.backgroundColor = UIColor.white 


     collectionView?.register(HeaderCell.self, forCellWithReuseIdentifier: "HeaderCell") 
     collectionView?.register(SummaryCell.self, forCellWithReuseIdentifier: "SummaryCell") 


    } 

    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { 
     if section == 0 { 
      return 1 
     } 
     return 1 
    } 

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

     if (indexPath.row == 0) { 

      let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCell", for: indexPath) as! HeaderCell 

      return cell1 

     } else { 
      let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "SummaryCell", for: indexPath) as! SummaryCell 

      return cell2 
     } 

    } 

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { 

     return CGSize(width: view.frame.width, height: 200) 

    } 

} 

가 나는 일을 얻을 수있는 this하지만 여전히 수 없습니다를 보았다.

P. 프로그래밍 방식으로 모든 것을 프로그래밍하고 있습니다.

+0

얼마나 많은 세포 컬렉션보기 indexPath.section에 그러나 나는 다음은 추가하여 제대로 작동하려면 얻을 수있는 제안의 일부를 사용 했습니까? __1__을 (를) 가지고 있으므로 'HeaderCell'이 (가) 나타납니다. – holex

+0

@Chace 다른 셀이 대신 표시하는 행을 전환하면 셀이 존재합니다. 설명해 주시겠습니까? –

+0

순간에 두 개의 셀 – Chace

답변

4

2 개의 셀을 등록했지만 1 개의 행만 반환합니다. 2 개의 항목을 반환하십시오.

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

아무런 대답도 올바르게/완전히 해결되지 않았습니다.

override func numberOfSections(in collectionView: UICollectionView) -> Int { 
    return 2 
} 

그리고 변화 indexPath.row

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

    if (indexPath.section == 0) { 

     let cell1 = collectionView.dequeueReusableCell(withReuseIdentifier: "HeaderCell", for: indexPath) as! HeaderCell 

     return cell1 

    } else { 
     let cell2 = collectionView.dequeueReusableCell(withReuseIdentifier: "SummaryCell", for: indexPath) as! SummaryCell 

     return cell2 
    } 
} 
+1

후행 else 문은 필요하지 않습니다. –