简体   繁体   English

如何自动重新加载tableview

[英]how to reload tableview automatically

Hi I am doing a post screen where users add post and view them.This is the viewcontroller for viewing.嗨,我正在做一个帖子屏幕,用户可以在其中添加帖子并查看它们。这是用于查看的视图控制器。 I am getting information from firebase and reload that info to my table view cells everything is going perfect.我正在从 firebase 获取信息并将该信息重新加载到我的表格视图单元格中,一切都很完美。 However to reload the screen and see if my posts are up to date I need to close the application and run it again each time although I use .reloadData() function and it doesn't reload automatically after each posting.但是,要重新加载屏幕并查看我的帖子是否是最新的,我需要关闭应用程序并每次再次运行它,尽管我使用 .reloadData() 函数并且每次发布后它不会自动重新加载。 here is my code where do I do the mistake?这是我的代码我在哪里做错了? or am I missing something?或者我错过了什么?

override func viewDidLoad() {
    super.viewDidLoad()
    self.navcik.title = "Posts"
    tableView = UITableView(frame:view.bounds, style: .plain)
    view.addSubview(tableView)
    let cellnib = UINib(nibName: "postTableCell", bundle: nil)
    tableView.register(cellnib, forCellReuseIdentifier: "postCell")

    tableView.delegate = self
    tableView.dataSource = self
    tableView.reloadData()
    observeposts()
}
func observeposts() {
    let db = Firestore.firestore()
    db.collection("posts").getDocuments { (snap, error) in
        var tempposts = [Post]()
        if error != nil {
            print("error:\(String(describing: error))")
        } else {
            for document in snap!.documents {
                if let dict = document.data() as? [String: Any] ,
                let username = dict["username"] as? String,
                let title = dict["title"] as? String,
                    let desc = dict["desc"] as? String {
                    let post = Post(id: "1", username: username, title: title, desc: desc)
                    tempposts.append(post)
                }

            }
            self.posts = tempposts
            self.tableView.reloadData()
        }

    }
}
@IBOutlet weak var navcik: UINavigationItem!

func numberOfSections(in tableView: UITableView) -> Int {
    return 1
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return posts.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "postCell", for: indexPath) as! postTableCell
    cell.set(post: posts[indexPath.row])
    return cell
}

好吧,我只是添加了一个复习并观察了那里的帖子并重新加载,现在工作完美。

try this尝试这个

 for document in snap!.documents {
            if let dict = document.data() as? [String: Any] ,
            let username = dict["username"] as? String,
            let title = dict["title"] as? String,
                let desc = dict["desc"] as? String {
                let post = Post(id: "1", username: username, title: title, desc: desc)
                tempposts.append(post)
            }
            tableView.reloadData()
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM