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
Please first check if you assigned a dataSource to the collectionView in Storyaboard
Add the below line in your viewDidLoad Method below the Array Declaration and your code will work.
and replace collection view with Your collectionView Name like below i have changed:
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.