ios - 重新加载数据时 CollectionView 闪烁

标签 ios swift flash parse-platform

我有一个 collectionView,其中填充了大约 60 张从 parse 下载的图像。可以更新这些图像,具体取决于是否已上传任何新图像。

但我的问题是在加载 View 后,我使用 PullToRefresh 函数刷新数据, Collection View 闪烁白色 然后再次显示图像...

这是给你看的视频:

https://www.youtube.com/watch?v=qizaAbUnzYQ&feature=youtu.be

我一整天都在尝试解决这个问题并找到解决方案,但我没有成功......!

这是我查询图像的方式:

 func loadPosts() {
     self.activityView.startAnimating()
        let followQuery = PFQuery(className: "Follows")
        followQuery.whereKey("follower", equalTo: PFUser.currentUser()!.username!)
        followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
                    if error == nil {
                        self.followArray.removeAll(keepCapacity: false)

                        for object in objects! {
                            self.followArray.append(object.valueForKey("following") as! String)

                        }

        let query = PFQuery(className: "Posts")
        query.limit = self.page
        query.whereKey("username", notContainedIn: self.followArray)
        query.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
        query.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in

            if error == nil {
                self.postImage.removeAll(keepCapacity: false)
                self.uuidArray.removeAll(keepCapacity: false)
                self.usernameArray.removeAll(keepCapacity: false)
                for object in objects! {
                    self.postImage.append(object.valueForKey("image") as! PFFile)
                    self.uuidArray.append(object.valueForKey("uuid") as! String)
                    self.usernameArray.append(object.valueForKey("username") as! String)
                }



            } else {
                print(error!.localizedDescription)
            }
            self.collectionView.reloadData()
            self.refresher.endRefreshing()
            self.activityView.stopAnimating()
            self.boxView.removeFromSuperview()


        })

           }
                    })
    }

这是我拉动刷新的方式:

override func viewDidLoad() {
        super.viewDidLoad()

        refresher.addTarget(self, action: "reload", forControlEvents: UIControlEvents.ValueChanged)
        collectionView.addSubview(refresher)
        loadPosts()

    }


    func reload() {

        collectionView.reloadData()
        refresher.endRefreshing()


    }

最佳答案

我假设每个帖子的 UUID 都是唯一的,因此您可以检查以前加载的计数是否与当前不同,然后您可以查看哪些帖子是新的,找出它们的索引路径然后只重新加载那些索引路径。我使用 sets 来确定添加了哪些 id,假设您不想两次显示相同的帖子,这将起作用。可能有更好的方法,但通常您需要执行类似于以下的操作:

 func loadPosts() {
         self.activityView.startAnimating()
            let followQuery = PFQuery(className: "Follows")
            followQuery.whereKey("follower", equalTo: PFUser.currentUser()!.username!)
            followQuery.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in
                        if error == nil {
                            self.followArray.removeAll(keepCapacity: false)

                            for object in objects! {
                                self.followArray.append(object.valueForKey("following") as! String)

                            }

            let query = PFQuery(className: "Posts")
            query.limit = self.page
            query.whereKey("username", notContainedIn: self.followArray)
            query.whereKey("username", notEqualTo: PFUser.currentUser()!.username!)
            query.findObjectsInBackgroundWithBlock ({ (objects:[PFObject]?, error:NSError?) -> Void in

                if error == nil {
                    let oldUUIDArray = self.uuidArray
                    self.postImage.removeAll(keepCapacity: false)
                    self.uuidArray.removeAll(keepCapacity: false)
                    self.usernameArray.removeAll(keepCapacity: false)
                    for object in objects! {
                        self.postImage.append(object.valueForKey("image") as! PFFile)
                        self.uuidArray.append(object.valueForKey("uuid") as! String)
                        self.usernameArray.append(object.valueForKey("username") as! String)
                    }
                    let uuidOldSet = Set(oldUUIDArray)
                    let uuidNewSet = Set(self.uuidArray)
                    let missingUUIDs = uuidNewSet.subtract(uuidOldSet)
                    let missingUUIDArray = Array(missingUUIDs)
                    let missingUUIDIndexPaths = missingUUIDArray.map{NSIndexPath(forItem:self.uuidArray.indexOf($0)!,inSe ction:0)} 

                    let extraUUIDs = uuidOldSet.subtract(uuidNewSet)
                    let extraUUIDArray = Array(extraUUIDs)
                    let extraUUIDIndexPaths = extraUUIDArray.map{NSIndexPath(forItem:oldUUIDArray.indexOf($0)!,inSection:0)}
                    self.collectionView.performBatchUpdates({
                    if extraUUIDIndexPath != nil {
                        self.collectionView.deleteItemsAtIndexPaths(extraUUIDIndexPaths)
                    }
                    if missingUUIDIndexPaths != nil {self.collectionView.insertItemsAtIndexPaths(missingUUIDIndexPaths)}
                    }, completion: nil)


                    } else {
                        print(error!.localizedDescription)
                    }

                self.refresher.endRefreshing()
                self.activityView.stopAnimating()
                self.boxView.removeFromSuperview()


            })

               }
                        })
        }

    func reload() {

            self.loadPosts()
            refresher.endRefreshing()


        }

关于ios - 重新加载数据时 CollectionView 闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587847/

相关文章:

java - 如何创建 Java 测验 GUI?

javascript - 当网站在非 Flash 浏览器中打开时重定向到 html 页面

ios - GHUnit 与 cocoa pod

iphone - 即使未启用推送通知,是否也可以获取设备 token ?

ios - 如果属性不是惰性变量(并且编译器没有告诉我这一点),为什么 #selector 函数不起作用?

string - Swift hashValue 使应用程序崩溃

swift - 字符串数组中的字符swift

ios - 保存、更新或对 iCloud 的任何操作不适用于临时或应用商店配置文件

ios - 为单个区域设置与其他区域不同的 IAP 定价

actionscript-3 - 在跨平台 AIR 应用程序中存储首选项数据的最简单方法是什么?