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

标签 ios swift iphone xcode uicollectionview

我有一个切换按钮。最初,它将处于关闭模式。因此,当我打开切换开关时,我需要将一组数据传递到 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/

相关文章:

iphone - NSFetchedResultsController sectionNameKeyPath 等效于 EKEventStore 从 iPhone 日历中获取的事件

ios - SwiftUI 关闭多个模态表

ios - 返回主线程时异步 UIView 内容不显示

ios - 在 Swift 中自己构造 NSError 对象的最佳方法是什么

ios - 在 Swift 中对象的生命周期内只调用一次方法

swift - 我如何更好地定义一个区域来检测拖放节点?

iPhone锁屏应用程序

ios - 在后台线程中处理 JPEG 图像

ios - 如何添加前导填充以在 UIStackView 中添加 View

iphone - 如何获取从标准 UIViewController 中调用的 UIWebView 的 UIScrollView 委托(delegate)方法?