gpt4 book ai didi

ios - 如何自动重新加载tableview

转载 作者:行者123 更新时间:2023-11-28 05:46:44 26 4
gpt4 key购买 nike

您好,我正在做一个帖子屏幕,用户可以在其中添加帖子并查看它们。这是用于查看的 View Controller 。我正在从 firebase 获取信息并将该信息重新加载到我的表格 View 单元格中,一切都很完美。然而,要重新加载屏幕并查看我的帖子是否是最新的,我需要关闭应用程序并每次再次运行它,尽管我使用 .reloadData() 函数并且它不会在每次发布后自动重新加载。这是我的代码我在哪里做错了?还是我遗漏了什么?

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
}

最佳答案

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

关于ios - 如何自动重新加载tableview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54394927/

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