Multiple Section in UICollectionView

后端 未结 2 453
甜味超标
甜味超标 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: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
          }
    
    }
    

提交回复
热议问题