Search code examples
iosswiftuicollectionviewuicollectionviewcell

How to use multiple arrays of items in one collectionView?


I have one UICollectionView in my UIView. What I'm trying to do is when user press the button then other items(images array) appear on same UICollectionView.

Let's say I have two arrays of items:

   let items = [UIImage(named: "moses-vega-436582-unsplash"), 
   UIImage(named: "april6"), UIImage(named: "april4"), UIImage(named: 
   "april5")]
   let items2 = [UIImage(named: "test01"), UIImage(named: "test02")]

Now when user press the button I want to update my collectionView with images from items2. I'm using basic code for collections (it's easy for me to detect which labels for example to show. Because I have variable called "Testas" and if it's 0 then I know that it's default collectionView and else it's .... :

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

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

        if Testas == 0 {
        cell.image.image = items[indexPath.item]
        if indexPath.item == 0 {
            cell.label.text = "One"
        }
        if indexPath.item == 1 {
            cell.label.text = "Two"
        }
        if indexPath.item == 2 {
            cell.label.text = "Collection 3"
        }
        if indexPath.item == 3 {
            cell.label.text = "Rainy Days"
        }
        } else {
            cell.image.image = items2[indexPath.item]
            if indexPath.item == 0 {
                cell.label.text = "White"
            }
            if indexPath.item == 1 {
                cell.label.text = "Blue"
            }
        }
        return cell
    }

In conclusion, I'm asking what I need to write to pass items2 into collectionView when the user press the button and how to make this collectionView appear? (because it's not a function or something what I could call easily I guess). Keep in mind that I have a function where it counts items. So that's the biggest problem. I need a function to count my items2 when user press the button and then make images appear. Thank you so much. Maybe it's not even possible to make what I want this way. I don't know.


Solution

  • You could accomplish this easily by creating an enum, and then simply toggling it's value when the button is pressed followed by a collectionView.reloadData()

    Here is your enum:

    enum ItemType : Int {
        case items = 0,
             items2 = 1
    }
    

    Declare it like this:

    var itemType : ItemType = .items
    

    Your collectionView functions would look something like this:

    func collectionView(_ collectionView: UICollectionView, 
       numberOfItemsInSection section: Int) -> Int {
        return itemType == .items ? items.count : items2.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    
        switch itemType {
        case .items:
            // code for items array
        default:
            // code for items2 array
        }
    
        return cell
    }
    

    Your button press:

    @IBAction func onButtonPressed(_ sender: Any) {
        itemType = .items2
        collectionView.reloadData()
    }
    

    If you have more than 2 arrays you will need to update your collectionView functions to something like this:

    func collectionView(_ collectionView: UICollectionView, 
       numberOfItemsInSection section: Int) -> Int {
        switch itemType {
        case .items:
            return items.count
        case .items2:
            return items2.count
        case .items3:
            return items3.count
        default:
            return items4.count
        }
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath) as! CollectionViewCell
    
        switch itemType {
        case .items:
            // code for items array
        case .items2:
            // code for items2 array
        case .items3:
            // code for items3 array
        default:
            // code for items4 array
        }
    
        return cell
    }