Multiple Section in UICollectionView

16,630

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
      }

}
Share:
16,630

Related videos on Youtube

christ2702
Author by

christ2702

I'm a programmer, a system analyst, a network administrator, a consultant, etc from Indonesia. I've graduated from University of Surabaya. My academic major is Computer Science with Artificial Intelligence concentration. I like to do some research about developing a program or just analyst my client's needed. I'm still a beginner who needs guidance and help from others who are more experienced. So please, don't hesitate to ask or help me, if I make a mistake. Thank you

Updated on June 04, 2022

Comments

  • christ2702
    christ2702 almost 2 years

    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 completed the code if it's just for 1 section. when I try to make it 2 sections, it always returns a nil value.

    please check my code below

    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
    
        if indexPath.section == 0 {
            var buttonSpecialist = cell.viewWithTag(1) as! UIButton
    
            buttonSpecialist.setTitle(list[indexPath.row].SpecialtyName, for: .normal)
    
            let btnimg = try? UIImage(data: Data(contentsOf: URL(string: list[indexPath.row].ImageUrl)!))
    
            var newimg = imageResize(image: btnimg as! UIImage, scaledTo: CGSize(width: 50, height: 50))
    
            buttonSpecialist.setImage(newimg, for: .normal)
            buttonSpecialist.contentMode = .center
            buttonSpecialist.imageView?.contentMode = .scaleAspectFit
    
            //let sign: Int = imageOnTop ? 1 : -1
            let imageSize: CGSize? = buttonSpecialist.imageView?.frame.size
            buttonSpecialist.titleEdgeInsets = UIEdgeInsetsMake(((imageSize?.height)! + 5) * 1, -(imageSize?.width)!, 0, 0)
            let titleSize: CGSize? = buttonSpecialist.titleLabel?.bounds.size
            buttonSpecialist.imageEdgeInsets = UIEdgeInsetsMake(-((titleSize?.height)! + 5) * 1, 0, 0, -(titleSize?.width)!)
    
            cell.layer.borderWidth = 1.0
            cell.layer.borderColor = UIColor.black.cgColor
        }
        else if indexPath.section == 1 {
            var buttonSpecialist = cell.viewWithTag(1) as! UIButton
    
            buttonSpecialist.setTitle(list2[indexPath.row].SpecialtyName, for: .normal)
    
            let btnimg = try? UIImage(data: Data(contentsOf: URL(string: list2[indexPath.row].ImageUrl)!))
    
            var newimg = imageResize(image: btnimg as! UIImage, scaledTo: CGSize(width: 50, height: 50))
    
            buttonSpecialist.setImage(newimg, for: .normal)
            buttonSpecialist.contentMode = .center
            buttonSpecialist.imageView?.contentMode = .scaleAspectFit
    
            //let sign: Int = imageOnTop ? 1 : -1
            let imageSize: CGSize? = buttonSpecialist.imageView?.frame.size
            buttonSpecialist.titleEdgeInsets = UIEdgeInsetsMake(((imageSize?.height)! + 5) * 1, -(imageSize?.width)!, 0, 0)
            let titleSize: CGSize? = buttonSpecialist.titleLabel?.bounds.size
            buttonSpecialist.imageEdgeInsets = UIEdgeInsetsMake(-((titleSize?.height)! + 5) * 1, 0, 0, -(titleSize?.width)!)
    
            cell.layer.borderWidth = 1.0
            cell.layer.borderColor = UIColor.black.cgColor
        }
    

    The main problem here is, I didn't know how to set the specific cell for the specific section. I already using 'IF' inside the function 'cellforindexpath', but it didn't worked.

    this is my return number of items in section

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 2
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
    
        if section == 0 {
            return list.count
        }
            else if section == 1{
                return list2.count
            }
    
        return 0
    
    }
    

    please help me

    • KKRocks
      KKRocks almost 7 years
      add numberofSection method .
    • alanlo
      alanlo almost 7 years
      What is the value you return in the method numberOfSections(in:)? Are you sure the code segment for the condition "indexPath.section == 1" is actually executed?
    • Aravind A R
      Aravind A R almost 7 years
      @christ2702 are you getting the cell as nil ?
  • christ2702
    christ2702 almost 7 years
    I want the same cell but, the content inside the cell was different
  • Dharma
    Dharma almost 7 years
    @christ2702 then simply put let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell1", for: indexPath) at the top method.
  • christ2702
    christ2702 almost 7 years
    1 more question. how to add section header in each section?
  • christ2702
    christ2702 almost 7 years
    please check my another problem too : stackoverflow.com/questions/44614743/…