简体   繁体   English

表格视图未使用Firebase更新

[英]Table view not updating with Firebase

I am trying to get a table view to update from my Firebase database. 我正在尝试从Firebase数据库获取要更新的表视图。 My data is structured like this (for reporting automobiles): 我的数据结构如下(用于报告汽车):

  1. Reports: 报告:
    • Randomly generated report ID number: 随机生成的报告ID号:
      • make: "make of car" 制造:“汽车制造”
      • model: "make of model" 型号:“模型制作”

I don't think that I am calling the data correctly. 我认为我没有正确调用数据。 I do not know how to select for the random report ID. 我不知道如何选择随机报告ID。 But there may be something else that I am missing. 但是我可能还缺少其他东西。 I am trying to get only the make and model of the vehicle to display in the text of the cell 我试图只将车辆的品牌和型号显示在单元格的文本中

var reportList:[String] = []

var ref: DatabaseReference!

var handle: DatabaseHandle?


@IBOutlet weak var reportsTableView: UITableView!

@IBAction func backButtonPressed(_ sender: UIBarButtonItem) {
    self.performSegue(withIdentifier: "reportsToHome", sender: self)
}

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return reportList.count

}


public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")

    cell.textLabel?.text = reportList[indexPath.row]

    return cell
}


override func viewDidLoad() {
    super.viewDidLoad()

    ref = Database.database().reference()

    handle = ref.child("Reports").observe(.childAdded, with: { (snapshot) in
        if (snapshot.value as? String) != nil
        {
            let make = String(describing: self.ref.child("Reports").child("make"))
            let model = String(describing: self.ref.child("Reports").child("model"))
            self.reportList.append(make + model)

            self.reportsTableView.reloadData()
        }
    }

    // Do any additional setup after loading the view.
)}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

} }

I'm not able to test this, but I have feeling that you were trying to cast the snapshot as a string. 我无法对此进行测试,但是我感觉到您正在尝试将快照转换为字符串。 Instead, you should cast it as a Dictionary so you can easily retrieve the data by key. 相反,您应该将其转换为Dictionary,以便可以轻松地通过键检索数据。

Try this code. 试试这个代码。 It sets the snapshot as a dictionary, and from there you are able to retrieve the make and model: 它将快照设置为字典,然后您可以从中检索品牌和型号:

override func viewDidLoad() {
super.viewDidLoad()

ref = Database.database().reference()

handle = ref.child("Reports").observe(.childAdded, with: { (snapshot) in
   if let reports = snapshot.value as? NSDictionary {
        var make = reports?["make"] as? String
        var model = reports?["model"] as? String

        self.reportList.append(make + model)
        self.reportsTableView.reloadData()
    }
}
)}

Going further, you can create a Report class: 更进一步,您可以创建一个Report类:

class Report: NSObject {
    var make: String?
    var model: String?
}

You can then set the make and model from the snapshot to create a new Report object. 然后可以从快照设置品牌和型号,以创建新的Report对象。

var make = reports?["make"] as? String
var model = reports?["model"] as? String
let newReport = Report()
newReport.setValuesForKeys(reports)

I hope this works, if not I'll look again. 我希望这能奏效,否则请再看一遍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM