gpt4 book ai didi

ios - 无法在按钮点击时重新加载 UICollectionView

转载 作者:行者123 更新时间:2023-11-29 05:52:09 26 4
gpt4 key购买 nike

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

这是我的代码:

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

}
}

当我运行我的应用程序时,当我关闭时 - 我的 Collection View 数据没有显示确切的数据..但它仍然显示以前的数据。当我再次将其完全关闭时。

对此有什么帮助吗?

最佳答案

添加一个新属性,例如 categoryarr,例如 categoryCollectionViewSource 并为其分配初始数据。在 Collection View 委托(delegate)方法中使用 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()
}
}

关于ios - 无法在按钮点击时重新加载 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55570313/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com