skip to Main Content

I’m making an onboarding screen for a project in which I’ve used storyboard for design and collection view to scroll steps of on boarding screen. No error in the code run but collection views items are not appearing..

import UIKit

class OnBoardingViewController: UIViewController {

@IBOutlet weak var ImageCollectionView: UICollectionView!
@IBOutlet weak var skipButton: UIButton!
@IBOutlet weak var pageControl: UIPageControl!
@IBOutlet weak var nextButton: UIButton!

   
var slides: [OnBoardingSlide] = []

override func viewDidLoad() {
    super.viewDidLoad()
    
    
    
    slides = [
        OnBoardingSlide(title: "This is the Title", description: "This is the description", image: UIImage(named: "onBoarding1")!),
        OnBoardingSlide(title: "This is the Title", description: "This is the description", image: UIImage(named: "onBoarding2")!),
        OnBoardingSlide(title: "This is the Title", description: "This is the description", image: UIImage(named: "onBoarding1")!)
    ]
    
}


@IBAction func nextButtonPressed(_ sender: UIButton) {
}

@IBAction func skipButtonPressed(_ sender: UIButton) {
}

}

extension OnBoardingViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return slides.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: OnBoardingCollectionViewCell.identifier, for: indexPath) as! OnBoardingCollectionViewCell
    cell.setup(slides[indexPath.row])
    return cell
}

}

2

Answers


  1. Please first check if you assigned a dataSource to the collectionView in Storyaboard

    Login or Signup to reply.
  2. Add the below line in your viewDidLoad Method below the Array Declaration and your code will work.

     DispatchQueue.main.async {
        self.ImageCollectionView.delegate = self 
        self.ImageCollectionView.dataSource = self
        self.ImageCollectionView.relaodData()
     }
     
    

    and replace collection view with Your collectionView Name like below i have changed:

     let cell = 
     self.ImageCollectionView.dequeueReusableCell(withReuseIdentifier: 
     OnBoardingCollectionViewCell.identifier, for: indexPath) as! 
     OnBoardingCollectionViewCell
    

    sometime your delegate works faster when array is getting the values if it still not work then you have to check your constraints of cell items.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search