How to programatically switch the data source of a collection view?

i Have successfully displayed different images using uicollectionview. Now I have a question, how do I dynamically switch the data source of a collection view using a few buttons ? Please help :slight_smile:

Thank you

Hello and welcome to the community!

On the tap of the button, you can programmatically change the data that the collection view is pointing to and then call the reloadData() method on it to make it request the data again from the view controller!

Wow finally I found the answer. Thank you Chris. Let me give it a try

Just a side note. Is protocol and delegate applicable in making this work?

import UIKit
import Messages

class MessagesViewController: MSMessagesAppViewController {

let stickerCategoryArray = ["Everyday1","Happy"]
var selectedCategory = "Everyday1"

@IBOutlet weak var StickerCollectionView: UICollectionView!

@IBAction func everydayButtonPressed(_ sender: Any) {
    selectedCategory = stickerCategoryArray[0]
    print(selectedCategory)
    reloadView()
}

@IBAction func happyButtonPressed(_ sender: Any) {
    selectedCategory = stickerCategoryArray[1]
    print(selectedCategory)
    reloadView()
}

func reloadView (){
    StickerCollectionView.reloadData()
}

override func viewDidLoad() {
    super.viewDidLoad()
    reloadView()
    // Do any additional setup after loading the view.
}

extension MessagesViewController: UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
10
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "StickerCell", for: indexPath) as! StickerCollectionViewCell
    
    cell.setupStickers(indexPath: indexPath, selectedCategory: selectedCategory)
    
    return cell
}

This is what I have come up with. My stickers did load properly but when i click on the buttons the collection view dosen’t reload. Did I call the reloadData at the wrong place?

Hello!
Did i miss it or did you set the datasource and delegate of the collection view somewhere other than the code you’re showing me?
Make sure you have this somewhere:

StickerCollectionView.datasource = self 
StickerCollectionView.delegate = self 

Thank you Chris. I used the click and drag in method to set the datasource and delegate for the collection view. Checking the outlets it looks alright. I was only wondering if I have applied the reloadData() correctly.

I have been trying to make this work for a very long time, first I tried protocol and delegate but later i learnt that its not used for this purpose. I really hope someone could point me to the correct path. Just point to me where the problem is. And it will help me a ton!

If you need the code you can download it here. https://www.dropbox.com/s/t11cy4maa9blkzb/testTest.zip?dl=0

uhmm protocols and delegates should make it work. the only difference would be on how you designed your code.

im assuming you have 2 or more “models” for you collection then, if that is the case the you can simply divide your code (row item, and row count) to have different scenarios based on what button was pressed, in short its like your repopulating your collectionview with new data

or you can do it by putting 2 collectionviews on top of each other and hiding/transitioning to the other collectionview based on button press (just like in the match up when the card was flipped)

got it, thank you for the answer Francis.