重新加载collectionview
时遇到问题。
当我removeAll()
然后fetchPosts()
时,它只会创建重复项,并且不会删除我认为已删除的帖子(暂时)。
我使用removeAll()
和fetchPosts()
的原因是“因为我有Firebase后端,所以它可以获取帖子,但发布图片后不会更新。
当我尝试self.collectionview.reloadData()
时,不会显示新帖子。
预先感谢!
这是我的代码:
@IBOutlet weak var collectionview: UICollectionView!
var posts = [Post]()
var following = [String]()
lazy var refreshControl: UIRefreshControl? = {
let refreshControl = UIRefreshControl()
refreshControl.tintColor = UIColor.gray
refreshControl.addTarget(self, action: #selector(refreshList), for: UIControlEvents.valueChanged)
collectionview.addSubview(refreshControl)
return refreshControl
}()
override func viewDidLoad() {
super.viewDidLoad()
setupNavigationBarItems()
fetchPosts()
collectionview.refreshControl = refreshControl
}
func setupNavigationBarItems() {
navigationItem.title = "Home"
}
@objc func refreshList() {
let deadline = DispatchTime.now() + .milliseconds(1000)
DispatchQueue.main.asyncAfter(deadline: deadline) {
self.self.posts.removeAll()
self.fetchPosts()
self.collectionview.reloadData()
print("reloading")
self.refreshControl?.endRefreshing()
}
}
@objc func fetchPosts() {
let ref = Database.database().reference()
ref.child("users").queryOrderedByKey().observeSingleEvent(of: .value, with: { snapshot in
let users = snapshot.value as! [String : AnyObject]
for (_,value) in users {
if let uid = value["uid"] as? String {
if uid == Auth.auth().currentUser?.uid {
if let followingUsers = value["following"] as? [String : String]{
for (_,user) in followingUsers{
self.following.append(user)
}
}
self.following.append(Auth.auth().currentUser!.uid)
ref.child("posts").queryOrderedByKey().observeSingleEvent(of: .value, with: { (snap) in
let postsSnap = snap.value as! [String : AnyObject]
for (_,post) in postsSnap {
if let userID = post["userID"] as? String {
for each in self.following {
if each == userID {
let posst = Post()
if let author = post["author"] as? String, let likes = post["likes"] as? Int, let pathToImage = post["pathToImage"] as? String, let postID = post["postID"] as? String {
posst.author = author
posst.likes = likes
posst.pathToImage = pathToImage
posst.postID = postID
posst.userID = userID
if let people = post["peopleWhoLike"] as? [String : AnyObject] {
for (_,person) in people {
posst.peopleWhoLike.append(person as! String)
}
}
self.posts.append(posst)
}
}
}
self.collectionview.reloadData()
}
}
})
}
}
}
})
ref.removeAllObservers()
}
func numberOfSections(in collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.posts.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "postCell", for: indexPath) as! PostCell
cell.postImage.downloadImage(from: self.posts[indexPath.row].pathToImage)
cell.authorLabel.text = self.posts[indexPath.row].author
cell.likeLabel.text = "\(self.posts[indexPath.row].likes!) Likes"
cell.postID = self.posts[indexPath.row].postID
for person in self.posts[indexPath.row].peopleWhoLike {
if person == Auth.auth().currentUser!.uid {
cell.likeBtn.isHidden = true
cell.dislikeBtn.isHidden = false
break
}
}
return cell
}
}
答案 0 :(得分:0)
在FetchPost函数中
@objc func fetchPosts() {
self.posts.removeAll()
}