【问题标题】:CollectionView flash when reloadData重新加载数据时 CollectionView 闪烁
【发布时间】:2016-06-05 21:33:48
【问题描述】:

我有一个 collectionView,其中填充了从 parse 下载的大约 60 张图像。这些图片可以根据是否上传了新图片进行更新。

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

这里有一个视频给你看:

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()


    }

【问题讨论】:

  • 您的重新加载函数实际上并没有再次获取数据。它只是丢弃所有的集合视图单元格并将相同的数据加载回其中。您需要调用 loadPosts,您应该检查数据是否不同,并且只有在调用 reloadData 或更好时才重新加载不同的项目。
  • @beyowulf 啊,听起来不错!我可以通过什么方式重新加载不同的项目?

标签: ios swift flash parse-platform


【解决方案1】:

我假设每个帖子的 UUID 都是唯一的,因此您可以检查以前加载的计数是否与当前不同,然后您可以查看哪些帖子是新的,找出它们的索引路径然后仅重新加载那些索引路径。我使用集合来确定添加了哪些 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()


        }

【讨论】:

  • 好的,谢谢!听起来很有希望..我只是复制并粘贴它,只是得到这两个小错误i.stack.imgur.com/nOxtz.png
  • 应该是let uuidNewSet = Set(self.uuidArray)
  • uuidNewArray 部分呢?那应该是NewSet吗?
  • 对不起应该是:` let missingUUIDIndexPaths = missingUUIDArray.map{NSIndexPath(forItem:self.uuidArray.indexOf($0)!,inSection:0)} `
  • i.stack.imgur.com/pqjad.png 不幸遇到了这个错误!不确定我以前是否见过它。我还在制作我的问题的视频,以帮助了解我的问题
猜你喜欢
  • 2021-06-17
  • 2012-06-19
  • 1970-01-01
  • 2013-02-18
  • 1970-01-01
  • 2023-03-22
  • 1970-01-01
  • 2017-07-19
  • 2013-10-08
相关资源
最近更新 更多