iosswiftxcodeuicollectionviewindexpath

Changing the title and the description below the collectionView cell When the collection view cell change. Doing this with index path is not working


Hope this pic helps descripe the caseI am updating the title and the description with the index path that i get when i cahnge the collection view index, But this is not doing the right things because when i scroll to end and start scrolling back the description and title associated with each cell shows the unintended ones.

class IntroPage : BaseViewController {

@IBOutlet weak var titleLabel: UILabel!
@IBOutlet weak var descriptionLabel: UILabel!
@IBOutlet weak var pageController: UIPageControl!
@IBOutlet weak var button: UIButton!
@IBOutlet weak var collectionView: UICollectionView!

private let reuseIdentifier = "CollectionViewCellIntroScene"

    let collectionViewImages : [UIImage] = [
#imageLiteral(resourceName: "maskGroup30"), 
#imageLiteral(resourceName: "maskGroup34"), 
#imageLiteral(resourceName: "maskGroup33")
]

let collectionViewTitleTexts : [String] = [
    "Track Your Fitness",
    "Win Exciting Deals",
    "Be a Challenge Winner!"
]

override func viewDidLoad() {
    super.viewDidLoad()
    collectionView.register(UINib(nibName: "CollectionViewCellIntroScene", bundle: nil), forCellWithReuseIdentifier: reuseIdentifier)
    renderCells()
}

func renderCells() {
    collectionView.isPagingEnabled = true
    collectionView.isScrollEnabled = true
    collectionView.showsVerticalScrollIndicator = false
    collectionView.showsHorizontalScrollIndicator = false
    let layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    layout.scrollDirection = .horizontal
    layout.minimumInteritemSpacing = 0
    layout.minimumLineSpacing = 0
    layout.sectionInset = UIEdgeInsets.zero
    collectionView.setCollectionViewLayout(layout, animated: true)
    collectionView.dataSource = self
    collectionView.delegate = self

}

}

Delegate for the Collection View

extension IntroPage : UICollectionViewDelegate {
func numberOfSections(in collectionView: UICollectionView) -> Int {
    return 1
}

func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
    return collectionViewImages.count
}
}

Data Source for Collection View

extension IntroPage : UICollectionViewDataSource {

func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {

    pageController?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

func scrollViewDidEndScrollingAnimation(_ scrollView: UIScrollView) {

    pageController?.currentPage = Int(scrollView.contentOffset.x) / Int(scrollView.frame.width)
}

Here when i scroll forward and then backward the title and description shows the wrong outputs

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCellIntroScene

    cell.image.image = collectionViewImages[indexPath.item]
    titleLabel.text = collectionViewTitleTexts[indexPath.item]
    
    return cell
}
}

Solution

  • You seem to be using a common titleLabel and descriptionLabel and setting the title and description in cellForItemAt, which is called when the cell is loaded. Move the following from IntroPage to your cell CollectionViewCellIntroScene (modify the xib by adding the title and desc labels):

    @IBOutlet weak var titleLabel: UILabel!
    @IBOutlet weak var descriptionLabel: UILabel!
    

    and in cellForRowAt:

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath) as! CollectionViewCellIntroScene
        cell.image.image = collectionViewImages[indexPath.item]
        cell.titleLabel.text = collectionViewTitleTexts[indexPath.item]
        return cell
    }