How to change data of a NSCollectionView with buttons

I have data that is coming in via JSON and I was able to switch the data in my collection view based off some logic. If I am logged in it shows one set of data and if I log out it shows another set of data. But now I want to change the data of my collection view based on button pushes. I have two buttons. One shows "All Products" and the other shows "Installed Products" How can I incorporate button pushes to switch between my data?


CollectionView Logic

extension ViewController: NSCollectionViewDataSource, NSCollectionViewDelegate, NSCollectionViewDelegateFlowLayout{
  
  
    func collectionView(_ collectionView: NSCollectionView, numberOfItemsInSection section: Int) -> Int {
      
        let prefs:UserDefaults = UserDefaults.standard
        let isLoggedIn:Int = prefs.integer(forKey: "ISLOGGEDIN") as Int
      
        if (isLoggedIn == 1) {
      
        return installedArray?.count ?? 0
        }
      
        return productsArray?.count ?? 0
    }
    func collectionView(_ collectionView: NSCollectionView, itemForRepresentedObjectAt indexPath: IndexPath) -> NSCollectionViewItem {
      
        let prefs:UserDefaults = UserDefaults.standard
        let isLoggedIn:Int = prefs.integer(forKey: "ISLOGGEDIN") as Int
      
        if (isLoggedIn == 1) {
      
        let item = collectionView.makeItem(withIdentifier: "Installed", for: indexPath) as! Installed
      
            item.buildProduct = installedArray?[indexPath.item]
      
        return item
        }
      
        let itemB = collectionView.makeItem(withIdentifier: "test1", for: indexPath) as! test1
      
        itemB.buildProduct = productsArray?[indexPath.item]
      
        return itemB
    }


Buttons

@IBAction func allAppsPushed(_ sender: Any) {
       
        let prefs:UserDefaults = UserDefaults.standard
        let isLoggedIn:Int = prefs.integer(forKey: "ISLOGGEDIN") as Int
       
        if (isLoggedIn == 1) {
           
           
          
           
        }else{
           
           
        }
       
    }
   
    @IBAction func installedPushed(_ sender: Any) {
       
        let prefs:UserDefaults = UserDefaults.standard
        let isLoggedIn:Int = prefs.integer(forKey: "ISLOGGEDIN") as Int
       
        if (isLoggedIn == 1) {
           
           
       
        }else{
           
        
           
        }
    }

I don't really follow what you are trying to do when you write "as! Installed". Do you have a class called "Installed", and one called "test1"?


However, if you want to make the NSCollectionView call your data source methods (collectionView(_: NSCollectionView, numberOfItemsInSection: Int) and collectionView(_: NSCollectionView, itemForRepresentedObjectAt: IndexPath)) again, then you have to call the reloadData() method on your collection view. So if your collection view is called collectionView, you call it like this:


self.collectionView.reloadData()


when you have changed the state that will make the data source methods produce a different result.

Yes I have 2 seperate .xib files that are representing my collection view items. But thank you for the answer. I will be testing this out!

I am still stuck on this problem. The only time I can change which set of data is shown is here:


if (isLoggedIn != 1) {
       
        let item = collectionView.makeItem(withIdentifier: "Installed", for: indexPath) as! Installed
       
            item.buildProduct = installedArray?[indexPath.item]
       
        return item
        }
       
        let itemB = collectionView.makeItem(withIdentifier: "test1", for: indexPath) as! test1
       
        itemB.buildProduct = productsArray?[indexPath.item]
       
        return itemB
      


When I try and run colView.reloadData() inside of my button based on the logic nothing happens. It either reloads the exact same data or doesnt switch.

I think i jsut figured it out. I needed to set isLoggedIn as a global variable and before i run my logic inside my button I needed to set my variable first:


@IBAction func allAppsPushed(_ sender: Any) {
      
        isLoggedIn = 1
      
        if (isLoggedIn == 1) {
          
            colView.reloadData()
          
        }else{
          
        }
    }
  
  
    @IBAction func installedPushed(_ sender: Any) {
      
        isLoggedIn = 0
  
        if (isLoggedIn == 1) {
          
            colView.reloadData()
         
        }else{
          
            self.colView.reloadData()
      
        }
    }


Now it switches between them perfectly!

How to change data of a NSCollectionView with buttons
 
 
Q