【问题标题】:How to change data of a NSCollectionView with buttons如何使用按钮更改 NSCollectionView 的数据
【发布时间】:2016-11-02 21:54:11
【问题描述】:

我有通过 JSON 传入的数据,我能够根据一些逻辑在我的集合视图中切换数据。如果我登录它会显示一组数据,如果我注销它会显示另一组数据。但现在我想根据按钮推送来更改我的集合视图的数据。我有两个按钮。一个显示“所有产品”,另一个显示“已安装的产品” 如何合并按钮以在我的数据之间切换?

CollectionView 逻辑

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

}

按钮

@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{



    }
}

【问题讨论】:

    标签: swift xcode macos swift3 nscollectionview


    【解决方案1】:

    尝试在您的按钮方法中调用collectionView.reloadData()。这将导致 collectionView 完全重新加载其数据,并且由于您的 isLoggedIn 标志已更改状态,您应该加载不同的数据。

    【讨论】:

    • 我明白你在说什么,但我的数据只有在我是否登录时才会改变。我不希望它依赖我是否登录。我需要完全根据按钮进行更改。我试过你说的,没有任何变化。它只是重新加载已经存在的相同数据并且不会切换。
    • 所以让你的按钮在你的视图控制器中改变一个实例变量,然后在集合视图的collectionView:cellForItemAtIndexPath:方法中,使用该实例变量的值来决定在集合视图中显示什么。
    • 你会碰巧有这样的例子吗?我有一个可行的解决方案,但我也对你的方法很好奇。
    【解决方案2】:

    我将我的 isLoggedIn 移到我的班级之外,使其成为一个全局变量,在每个按钮中我首先设置变量,然后检查我的逻辑,现在它在我的数据之间切换!

    @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{
    
            colView.reloadData()
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2022-07-29
      • 1970-01-01
      • 1970-01-01
      • 2016-07-08
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多