Swift 中的 UICollectionView 自定义布局重新加载问题 [英] UICollectionView Custom layout reloading issue in Swift

查看:55
本文介绍了Swift 中的 UICollectionView 自定义布局重新加载问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是参考链接:

https://www.raywenderlich.com/107439/uicollectionview-自定义布局教程-pinterest

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

I have implemented load more in UICollectionView at last cell , the data is getting downloaded, after download complete i want to reload collection

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 reloaddata

like this collectionview reloadata

有人可以帮我吗?

推荐答案

遇到您的问题,目前我正面临同样的问题,经过一番挖掘后我想通了!

Ran into your question and am currently facing the same problem, after some digging i figured it out!

问题出在 PintrestLayout 文件中

在里面你会发现类似 (PROBLEM) 的代码:

Inside you will find code like (PROBLEM) :

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

基本上,如果您应用到 CollectionView 的属性是空的(它们在开始时),将 PintrestLayout 应用到它,那就完美了!但是如果你重新加载数据并添加到collectionView,你需要再次应用布局,但是当你再次尝试将布局应用到collectionView时缓存不是空的,因此它返回并且不会根本没用...

Basically, if the attributes that you are applying to the CollectionView are empty (they are at the start), apply the PintrestLayout to it, and thats perfect! BUT If you are reloading the data and adding to the collectionView, you need to apply the layout again, but the cache isnt empty when you try to apply the layout to the collectionView again, thus it returns and doesn't work at all...

解决方案:

将该代码替换为:

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

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

Now you are saying "i dont care if its empty or not, just apply the damn layout to my collectionView"

大功告成!现在 reloadData() 将更新您获得的单元格数量...

And Done! Now reloadData() will update how many cells you got...

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

This is pretty expensive though, if you wanna make it faster, and trim some memory etc... Then look into invalidateFlowLayout Documentation.

这篇关于Swift 中的 UICollectionView 自定义布局重新加载问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆