我目前有一个错误在表视图中显示我的数据。我设法显示细胞,但是没有确切的细胞的数量和数据没有显示在显示。如何在UILabel中显示firebase数据?谢谢。
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()
}
})
}
}
细胞tableview
检查fetchActivityList()"方法通过在其中放置断点来验证您是否在其中获得值。检查您在快照中获得的值,以及是否所有可选变量都有值。
问题解决了。我只是改变了这些代码行
func fetchActivityList() {
let ref = Database.database().reference()
ref.child("Activities").child(userID!).observeSingleEvent(of: .value) { (snapshot) in
let results = snapshot.value as? NSDictionary
let titre = results?["Title"] as? String ?? "Pas de titre"
let heure = results?["Heure"] as? String ?? "Pas d'heure"
let date = results?["Date"] as? String ?? "Pas de Date"
let location = results?["Location"] as? String ?? "Pas d'emplacement"
let event = results?["EventType"] as? String ?? "Pas d'évènement"
let comment = results?["TexField"] as? String ?? "Pas de commentaires"
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?)
print(heure)
print(titre)
print(date)
print(location)
print(event)
print(comment)
self.activityList.append(myAct)
DispatchQueue.main.async {
self.TableView!.reloadData()
}
}
}
}