【问题标题】:Reload the collection view with refresh使用刷新重新加载集合视图
【发布时间】:2018-11-05 08:51:57
【问题描述】:

我想刷新视图控制器中的数据。我使用 reloadData 但它不起作用。我该怎么办?请帮助我。我在标签栏控制器中有这个视图控制器。我试图从数组中删除所有元素并重用该函数,但我将元素加倍。我曾尝试使用 viewwillappear 但每次他返回 Viewcontroller 时,它都会多次添加相同的项目

override func viewDidLoad() {
    super.viewDidLoad()
    refreshControl.addTarget(self, action: #selector(HomeViewController.refreshData), for: UIControlEvents.valueChanged)
    collectionview.addSubview(refreshControl)
    lodPosts()
}

@objc func refreshData() {
    refreshControl.endRefreshing()
}

func lodPosts() {
    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["uid"] 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["photoUrl"] as? String, let postID = post["postID"] as? String,let profileImage = post["profileImageUrl"] as? String,let caption = post["caption"] as? String {
                                            posst.author = author
                                            posst.likes = likes
                                            posst.pathToImage = pathToImage
                                            posst.postID = postID
                                            posst.userID = userID
                                            posst.userimage = profileImage
                                            posst.caption = caption
                                            self.posts.append(posst)

                                        }

                                    }
                                }
                                self.collectionview.reloadData()

                            }
                        }

                    })

                }
            }
        }

    })
}

【问题讨论】:

    标签: swift


    【解决方案1】:

    所有 UI 操作都必须在主线程上执行

    斯威夫特 4

    DispatchQueue.main.async {
        self.collectionview.reloadData()
    }
    

    【讨论】:

    • 我把这段代码放在函数中(refreshData)但是刷新时它不起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-03
    • 2013-06-16
    • 1970-01-01
    • 2011-04-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多