user10969500
user10969500

Reputation:

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. 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. 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
}

Upvotes: 1

Views: 1054

Answers (2)

user10969500
user10969500

Reputation:

Well I just added a refresher and observed the posts there and reloaded, works perfect now.

Upvotes: 1

Nishant Pathania
Nishant Pathania

Reputation: 349

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()
        }

Upvotes: 0

Related Questions