UICollectionView Swift中的自定义布局重新加载问题

时间:2016-08-03 13:11:09

标签: swift uicollectionviewcell uicollectionviewlayout

这是参考链接:

https://www.raywenderlich.com/107439/uicollectionview-custom-layout-tutorial-pinterest

我已经在UICollectionView的最后一个单元格中实现了更多的加载,数据正在下载,下载完成后我想重新加载收藏

collectionView.collectionViewLayout.invalidateLayout()
self.collectionView.reloadData()

let concurrentQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_async(concurrentQueue) {
            DataManager.sharedInstance.homeRequest(Dictionary: params, onSuccess: { (dict) in
                self.downloadPageIndex +=  1
                let response = HomeObject(dictionary: dict)

                for (_,e) in (response.dataDict?.enumerate())!{
                    self.dataArray.append(e)
                }

                dispatch_async(dispatch_get_main_queue()) {

                    self.collectionView.reloadData()
                    self.collectionView.collectionViewLayout.invalidateLayout()

                }
                self.activityIndicatorCell.stopAnimating()

                }, onError: { (error) in
                    self.activityIndicatorCell.stopAnimating()

            })

喜欢这个collectionview reloadata

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

进入你的问题,目前我正面临同样的问题,经过一番挖掘后,我发现了它!

问题出在PintrestLayout文件

在里面你会找到类似(问题)的代码:

guard cache.isEmpty == true, let collectionView = collectionView else { return }

基本上,如果您应用于CollectionView的属性为空(它们位于开头),请将PintrestLayout应用于它,这就完美了! 但是如果要重新加载数据并添加到collectionView,则需要再次应用布局,但是当您尝试再次将布局应用于collectionView时缓存不为空,因此它返回并且不会#39; t work at all ...

解决方案

用以下代码替换该代码:

guard cache.isEmpty == true || cache.isEmpty == false, let collectionView = collectionView else {
        return
    }

现在你说"我不在乎它是否为空,只是将该死的布局应用到我的collectionView"

完成了!现在reloadData()将更新您获得的细胞数量......

这是非常昂贵的,如果你想让它更快,并修剪一些内存等...然后查看invalidateFlowLayout文档。