I currently have an error displaying my data in a tableview. I managed to display the cells, but there is not the exact number of cells displayed and the data is not displayed in it.
How to display firebase data in UILabel?
Thanks.
struct List {
var heure: String?
var date: String?
var location: String?
var event: String?
var title: String?
var comment: String?
init(heure: String?, date: String?, location: String?, event: String?, title: String?, comment: String?) {
self.heure = heure
self.date = date
self.location = location
self.event = event
self.title = title
self.comment = comment
}
}
class FourthViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var Button: UIButton!
@IBOutlet weak var TableView: UITableView!
var activityList = [List]()
let userID = Auth.auth().currentUser?.uid
override func viewDidLoad() {
super.viewDidLoad()
let nib = UINib(nibName: "TableViewCell", bundle: nil)
TableView.register(nib, forCellReuseIdentifier: "TableViewCell")
TableView.dataSource = self
TableView.delegate = self
TableView.layer.cornerRadius = 20
fetchActivityList()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return activityList.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "TableViewCell", for: indexPath) as! TableViewCell
let test = activityList[indexPath.row]
cell.Title?.text = test.title
cell.Heure?.text = test.heure
cell.Date?.text = test.date
cell.Location?.text = test.location
cell.Event?.text = test.event
cell.Comment?.text = test.comment
return cell
}
func fetchActivityList() {
let ref = Database.database().reference()
ref.child("Activities").child(userID!).observe(.childAdded, with: { (snapshot) in
let results = snapshot.value as? [String : AnyObject]
let titre = results?["Title"]
let heure = results?["Heure"]
let date = results?["Date"]
let location = results?["Location"]
let event = results?["EventType"]
let comment = results?["Comment"]
let myAct = List(heure: heure as! String?, date: date as! String?, location: location as! String?, event: event as! String?, title: titre as! String?, comment: comment as! String?)
self.activityList.append(myAct)
DispatchQueue.main.async {
self.TableView!.reloadData()
}
})
}
}
2
Answers
The problem is solved. I just changed these lines of code
Check your "fetchActivityList()" method by putting break points in it to verify if you are getting values in it.
Check what values you are getting in snapshot and if all the optional variables have values.