Multiple Section in UICollectionView

后端 未结 2 454
甜味超标
甜味超标 2021-01-07 10:08

I was building an iOS app for my hospital using collection view. However, I need to use multiple sections for the specialist clinic depends on the purpose. I already complet

相关标签:
2条回答
  • 2021-01-07 10:22

    If you have different cells layout requirement in that case you should use different custom cells with different reuse identifier. As I can see in your code you have same layout of cell with different datasource.

    So I would say try to subclass the collectionview cell and put a IBOutlet property for button through xib or storyboard in the subclass.

    You can refer this question: how to create custom UICollectionViewCell

    0 讨论(0)
  • 2021-01-07 10:40

    Make sure assigning DataSource & Delegate to CollectionView

    1.Give number of sections you wanna show using below method

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

    2.Set item count of each sections for CollectionView using below method.

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
       return (section == 0) ? list.count : list2.count
    }
    

    3.Assigning cell for each item to CollectionView.

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    
        //If you are using multiple cells based on section make condition 
    
         if indexPath.section == 0 {
                 //make sure the identifier of your cell for first section
                 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath)
                // do your stuffs
                 return cell
         }else{
                 //make sure the identifier of your cell for second section
                 let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell2", for: indexPath)
                // do your stuffs
                 return cell
          }
    
    }
    
    0 讨论(0)
提交回复
热议问题