skip to Main Content

How is it possible to change numberOfItemsInSection parameter of a collection view?

I have made a basic setup of a collection view. And now I try to make a button, that changes the amount of items in it.

The general setup is a standard one:

   func numberOfSections(in collectionView: UICollectionView) -> Int {
        1
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        arrayA.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell            
        cell.imageView.image = arrayA[indexPath.item][0].image
        return cell
    }

The question is – how to configure a button, so it could change numberOfItemsInSection parameter from current arrayA.count to some other (e.x. arrayB.count)?

Example: enter image description here

2

Answers


  1. // No need of numberOfSection because it is bydefault 1.
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        arrayA.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
        cell.imageView.image = arrayA[indexPath.item][0].image
        return cell
    }
    
    @IBAction func changeArrayCount(sender: UIButton) {
        arrayA.append(image)
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }
            self.collectionView.reloadData()
        }
    }
    
    Login or Signup to reply.
  2. You could take a common a flag to toggle between arrayA and arrayB. When the button is clicked as

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return isUseArrayA ? arrayA.count : arrayB.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell           
        cell.imageView.image = isUseArrayA ? arrayA[indexPath.item][0].image : arrayB[indexPath.item][0].image
        return cell
    }
    
    @IBAction func changeSource(sender: UIButton) {
        if sender.tag == 0 {
           isUseArrayA = true
           sender.tag = 1
        } else {
           sender.tag = 0
           isUseArrayA = false
        } 
        DispatchQueue.main.async { [weak self] in
            guard let self = self else { return }
            self.collectionView.reloadData()
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search