gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 23:00:54 27 4
gpt4 key购买 nike

我有一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com