CollectionView在刷新reloadData时闪现 [英] CollectionView flash when reloadData

查看:519
本文介绍了CollectionView在刷新reloadData时闪现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个collectionView,里面大约有60个图像从解析下载。这些图像可以根据是否已经上传新的更新。



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



这里有一个视频向您展示:

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



我一直在努力解决这一整天&找到一个解决方案,但我没有成功..!

继承人如何查询图像:



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

对象中的对象!{
self.followArray.append(object.valueForKey(following)as !字符串)

}

let query = PFQuery(className:Posts)
query.limit = self.page
query.whereKey username,notContainedIn:self.followArray)
query.whereKey(username,notEqualTo:PFUs )
query.findObjectsInBackgroundWithBlock({(objects:[PFObject]?,error:NSError?) - >无效

如果错误== nil {
self.postImage.removeAll(keepCapacity:false)
self.uuidArray.removeAll(keepCapacity:false)
self。 usernameArray.removeAll(keepCapacity:false)对于对象中的
! {
self.postImage.append(object.valueForKey(image)as!PFFile)
self.uuidArray.append(object.valueForKey(uuid)as!String)
self .usernameArray.append(object.valueForKey(username)as!String)
}


$ b} else {
print(error !. localizedDescription)

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


})

}
})
}
$ b $ p

这里是我如何刷新:

  override func viewDidLoad(){
super.viewDidLoad()

refresher.addTarget(self,action:reload,forControlEvents:UIControlEvents.ValueChanged)
collecti onView.addSubview(刷新)
loadPosts()



$ b func reload(){

collectionView.reloadData ()
refresher.endRefreshing()

$ b}


我假设每个帖子的UUID都是唯一的,所以你可以检查前一次加载的计数是否与当前不同,然后你可以看到哪些帖子是新的,找出他们的索引路径,然后只重新加载这些索引路径。我用集合来确定哪些id已被添加,这将工作假设你不想显示相同的职位两次。有可能是一个更好的方法,但一般来说,你需要做类似于以下内容:
$ b $ pre $ func loadPosts ){
self.activityView.startAnimating()
let followQuery = PFQuery(className:Follows)
followQuery.whereKey(follower,equalTo:PFUser.currentUser()!. username !)
followQuery.findObjectsInBackgroundWithBlock({(objects:[PFObject]?,error:NSError?) - >无效
如果错误== nil {
self.followArray.removeAll(keepCapacity :false)

用于对象中的对象!{
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?) - >无效

如果错误== nil {
let oldUUIDArray = self.uuidArray
self.postImage.removeAll(keepCapacity:false)
self.uuidArray.removeAll keepCapacity:false)
self.usernameArray.removeAll(keepCapacity:false)
对象中的对象! {
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)
让missingUUIDs = uuidNewSet.subtract(uuidOldSet)
let missingUUIDArray = Array(missingUUIDs)
let missingUUIDIndexPaths = missingUUIDArray.map {NSIndexPath(forItem:self.uuidArray.indexOf($ 0)!,inSe ction:0) }
$ b $ 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)}
},完成:无)


} else {
print(error!.localizedDescription)
}

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


))





func reload(){

self.loadPosts()
refresher.endRefreshing()


}


I have a collectionView which is populated with around 60 images downloading from parse. These images can be updated depending if any new ones have been uploaded.

But my problem is after I load the view, and I refresh the data using PullToRefresh function, the collection view Flashes white and then displays the images again...

here's a video to show you :

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

I have been trying to fix this all day & find a solution, but I have had no success..!

Heres how I'm querying the images :

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


        })

           }
                    })
    }

And here is how I am pulling to refresh:

override func viewDidLoad() {
        super.viewDidLoad()

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

    }


    func reload() {

        collectionView.reloadData()
        refresher.endRefreshing()


    }

解决方案

I assume that the UUID's are unique for every post, so you can check to see if the count from the previous load is different from the current, then you can see which posts are new, figure out their index path then only reload those index paths. I used sets to determine which id's had been added, which will work assuming you don't want to display the same post twice. There might be a better way of doing it, but in general you need to do something similar to the following:

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


        }

这篇关于CollectionView在刷新reloadData时闪现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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