Reputation:
I am trying to retrieve my data from the Firebase Realtime Database, however, the data is not appearing in my UITableView
. I am retrieving the data and trying to append it to an array that is of type of a data model I have created called Party
. I am not sure how to setup up my code so I can successfully get the data from Firebase to my UITableView
and I am also not sure where the problem lies.
I have tried reording my code and using different methods to retrieve data from Firebase but nothing worked. I created an array called parties which store information of type Party
.
The following is my code for the override function of the UITableViewController
. This is where most of the code for my Firebase Database rests. This is a table view with custom cells.
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
// Set the Firebase reference.
ref = Database.database().reference()
// Retrieve the parties and listen for changes.
ref?.child("parties").observeSingleEvent(of: .value, with: { (snapshot) in
// Take the value from the snapshot and add it to the parties array.
let value = snapshot.value as? NSDictionary
let titleText = value?["titleText"] as? String ?? "Title"
let dateText = value?["dateText"] as? String ?? "Date"
let timeText = value?["timeText"] as? String ?? "Time"
self.parties.append(Party(title: titleText, date: dateText, time: timeText))
})
tableView.reloadData()
guard let cell = tableView.dequeueReusableCell(withIdentifier: "YourPartiesTableViewCell", for: indexPath) as? YourPartiesTableViewCell else {
fatalError("The dequeued cell is not an instance of YourPartiesTableViewController")
}
let party = parties[indexPath.row]
// Configure the cell
cell.titleLabel.text = party.titleText
cell.dateAndTimeLabel.text = "\(party.dateText) at \(party.timeText)"
cell.inviationsLabel.text = "0 people"
cell.view.layer.cornerRadius = 15
return cell
}
I expected it to append the data from Firebase to the parties array. Then, I wanted that data to show on the table view. Why is that not happening? Where am I wrong in my code?
Upvotes: 0
Views: 41
Reputation: 100503
You shouldn't load any data inside cellForRowAt
as it's called for every instance inside the dataSource array and remove reloadData
also as not cause recursive calls
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return parties.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "YourPartiesTableViewCell", for: indexPath) as? YourPartiesTableViewCell else {
fatalError("The dequeued cell is not an instance of YourPartiesTableViewController")
}
let party = parties[indexPath.row]
// Configure the cell
cell.titleLabel.text = party.titleText
cell.dateAndTimeLabel.text = "\(party.dateText) at \(party.timeText)"
cell.inviationsLabel.text = "0 people"
cell.view.layer.cornerRadius = 15
return cell
}
func loadData() {
// Set the Firebase reference.
ref = Database.database().reference()
// Retrieve the parties and listen for changes.
ref?.child("parties").observeSingleEvent(of: .value, with: { (snapshot) in
// Take the value from the snapshot and add it to the parties array.
let value = snapshot.value as? NSDictionary
let titleText = value?["titleText"] as? String ?? "Title"
let dateText = value?["dateText"] as? String ?? "Date"
let timeText = value?["timeText"] as? String ?? "Time"
self.parties.append(Party(title: titleText, date: dateText, time: timeText))
self.tableView.reloadData()
})
}
Call loadData
from viewDidLoad
Upvotes: 1