UICollectionView ReloadData无法正确显示内容

问题描述 投票:0回答:1

我正在尝试使用SegmentControl过滤homeCollectionView。敲击该段时,我正在根据词典中可用的标签过滤内容。当我执行ReloadData并在各段之间切换时,第一次(第一次点击这些段时),过滤器正在工作并且所有数据都在传输,但是当我再次点击这些段时,单元格中的内容,尤其是LabelViews文本之后没有显示。此外,随机indexPath也会发生这种情况。

这是我的代码:

@objc func toggleHomeContent(_ notification: NSNotification) {
    toggleValContType = notification.object as? String ?? "all"

    if (toggleValContType == "all") {
        mainArrayData = primaryArrayData
    }
    else if (toggleValContType == "collections") {
        mainArrayData = primaryArrayData { $0["filterType"] == "Col" || $0["filterType"] == "CTA" }
    }
    else if (toggleValContType == "books") {
        mainArrayData = primaryArrayData { $0["filterType"] == "Book" || $0["filterType"] == "CTA" }
    }

    homeCollectionView?.reloadData()
    homeCollectionView?.layoutIfNeeded()
    homeCollectionView?.collectionViewLayout.invalidateLayout()
    //DispatchQueue.main.async(execute: homeCollectionView.reloadData)
}

并且按数组这样声明:

var mainArrayData   : [[String:String]] = HomeArray().mainArray
var primaryArrayData: [[String:String]] = HomeArray().mainArray

这里是问题的快照:

Snapshot of the issue

提前感谢!

ios swift uicollectionview uicollectionviewcell reloaddata
1个回答
0
投票

在主线程中添加此代码:

 homeCollectionView?.reloadData()
homeCollectionView?.layoutIfNeeded()

DispatchQueue.main.async {
 homeCollectionView?.reloadData()
homeCollectionView?.layoutIfNeeded()}
© www.soinside.com 2019 - 2024. All rights reserved.