Reputation: 21
There I am having a problem reloading a tableView once a value has been appended to an array.
Here's my code
//
// HomeViewController.swift
// um
//
// Created by Arnav on 17/09/18.
// Copyright © 2018 Arnav. All rights reserved.
//
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth
class HomeTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var questions = [Question]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// var postQuestion = Question(questionName: "Hi", created_by: "Bye")
// print(postQuestion.name)
// print(postQuestion.created)
loadQuestions()
}
var list = ["milk", "bread"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
func loadQuestions() {
Database.database().reference().child("questionPosts").queryOrderedByKey().observe(.childAdded) { (snapshot) in
if let dict = snapshot.value as? NSDictionary {
//var questionName = dict["name"] as! String
//var created_by = dict["email"] as! String
let questionTitle = dict["name"] as? String ?? ""
let created_by = dict["email"] as? String ?? ""
self.list.append(questionTitle)
}
}
}
This code is just added a new value to the list array and the table view displays whats from the list array but when I append something new to the list array and run it on the simulator the new value doesn't
Upvotes: 0
Views: 3147
Reputation: 45
In my case, I remove all items in a previous array then reload in again.
I tried "didset" to reload anytime an array changed but no luck.
So, simply do
yourarray.removeall()
tableview.reloadData()
Upvotes: 0
Reputation: 1849
You need to create an outlet for your tableview and once your data is loaded you can call
tableview.reloadData()
Alternatively, you can cell the above function on the didSet
of your array like below:
var questions = [Question]() {
didSet {
// because we perform this operation on the main thread, it is safe
DispatchQueue.main.async {
self?.tableView.reloadData()
}
}
didSet
will be called every time you change something in your questions array
Upvotes: 1
Reputation: 113
After adding the new element to the list
, call insertRowAtIndexPaths:withRowAnimation
on your tableView as follows:
self.list.append(questionTitle)
let indexPath = IndexPath(row:self.list.count - 1, section:0)
tableView.insertRowsAtIndexPaths([indexPath], withRowAnimation:.default)
Upvotes: 2
Reputation: 307
You can use the property observer didSet
.
var questions = [Question]() {
didSet {
Dispatch.main.async {
self.tableView.reloadData()
}
}
}
For more information click here.
Upvotes: 2
Reputation:
import UIKit
import Firebase
import FirebaseDatabase
import FirebaseAuth
class HomeTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var questions = [Question]()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
// var postQuestion = Question(questionName: "Hi", created_by: "Bye")
// print(postQuestion.name)
// print(postQuestion.created)
loadQuestions()
yourtableView.reloadData()
}
var list = ["milk", "bread"]
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return list.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell(style: UITableViewCellStyle.default, reuseIdentifier: "cell")
cell.textLabel?.text = list[indexPath.row]
return cell
}
func loadQuestions() {
Database.database().reference().child("questionPosts").queryOrderedByKey().observe(.childAdded) { (snapshot) in
if let dict = snapshot.value as? NSDictionary {
//var questionName = dict["name"] as! String
//var created_by = dict["email"] as! String
let questionTitle = dict["name"] as? String ?? ""
let created_by = dict["email"] as? String ?? ""
self.list.append(questionTitle)
}
}
}
Upvotes: 0