点击按钮后无法重新加载UICollectionView

时间:2019-04-08 09:33:24

标签: ios swift iphone xcode uicollectionview

我有一个切换按钮。最初,它将处于关闭模式。因此,当我打开切换开关时,我需要将一组数据传递到集合视图。当再次关闭时,我需要将一组数据传递到集合视图。

这是我的代码:

@IBAction func cateSelTap(_ sender: Any) {

     isChecked = !isChecked
     if isChecked {
        self.subsetTheCategoryListForClientLogin(isCityLogin: false)
     }else {
        self.subsetTheCategoryListForClientLogin(isCityLogin: true)
     }
}

    func subsetTheCategoryListForClientLogin(isCityLogin: Bool) {
        let cityType = "city"
        print("LOG:CATE1 \(self.categoryarr)")
        var objectIds = [Int]()
        var subsetArray = NSMutableArray()
        for i in 0..<self.categoryarr.count {
            let type = (self.categoryarr.object(at:i) as AnyObject).value(forKey: "type") as! String
            if isCityLogin {
                if type == cityType {
                    subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
                    objectIds.append(i)
                }
            } else {
                if type != cityType {
                    subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
                    objectIds.append(i)
                }
            }
        }
        self.categoryarr.removeAllObjects()
        self.categoryarr = subsetArray
        print("LOG:CATE2 \(self.categoryarr)")
        DispatchQueue.main.async{
            self.categorycollection.reloadData()
//            self.categorycollection.performSelector(onMainThread: #selector(self.categorycollection.reloadData), with: nil, waitUntilDone: true)

        }
    }

当我运行我的应用程序时,以及当我关闭应用程序时-我的收藏夹视图数据未显示确切的数据。仍然显示先前的数据。同样,当我关闭其完全空白时。

对此有任何帮助吗?

2 个答案:

答案 0 :(得分:0)

@IBAction func cateSelTap(_ sender: Any) {
      isChecked = !isChecked
   if isChecked {
         self.subsetTheCategoryListForClientLogin(isCityLogin: false)
   } else {
         self.subsetTheCategoryListForClientLogin(isCityLogin: true)
   }
}

答案 1 :(得分:0)

添加一个新属性,例如categoryarr,例如categoryCollectionViewSource并为其分配初始数据。在集合视图委托方法中使用categoryCollectionViewSource而不是categoryarr。然后在过滤方法中

func subsetTheCategoryListForClientLogin(isCityLogin: Bool) {

    let cityType = "city"
    print("LOG:CATE1 \(self.categoryarr)")
    var objectIds = [Int]()
    var subsetArray = NSMutableArray()
    for i in 0..<self.categoryarr.count {
        let type = (self.categoryarr.object(at:i) as AnyObject).value(forKey: "type") as! String
        if isCityLogin {
            if type == cityType {
                subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
                objectIds.append(i)
            }
        } else {
            if type != cityType {
                subsetArray.add(self.categoryarr.object(at:i) as AnyObject)
                objectIds.append(i)
            }
        }
    }
    //note this line
    self.categoryCollectionViewSource.removeAllObjects()
    self.categoryCollectionViewSource = subsetArray
    print("LOG:CATE2 \(self.categoryarr)")
    DispatchQueue.main.async{
        self.categorycollection.reloadData()
    }
}