reloadData

时间:2016-02-23 20:47:40

标签: ios swift flash parse-platform

我有一个collectionView,其中包含从解析下载的大约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()


    }

1 个答案:

答案 0 :(得分: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()


        }