如何显示同一用户的2个tableViews? Firebase数据库

时间:2018-09-26 06:37:32

标签: firebase firebase-realtime-database tableview

Firebase数据库

Firebase Database

driver1主页

driver1 Main page

丰田汽车详细信息

toyota car details

最终输出

enter image description here 所以问题是driver1有2辆车。我该如何在tableView中显示丰田汽车信息和马自达汽车信息。

我可以通过以下代码显示驾驶员1的汽车清单:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "driverRequestCell", for: indexPath)

if let email = Auth.auth().currentUser?.email {

Database.database().reference().child("Driver").queryOrdered(byChild: "email").queryEqual(toValue: email).observe(.childAdded, with: { (snapshot) in

    let snapshot = self.driverRequests[indexPath.row]

    if let driverRequestDictionary = snapshot.value as? [String:AnyObject] {

    if let typeOfCar = driverRequestDictionary["car"] as? String {

            cell.textLabel?.text = typeOfCar

        }
    }
})
}
return cell

}

所以我当前的didSelectRowAt代码是:

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let snapshot = driverRequests[indexPath.row]

    performSegue(withIdentifier: "carDetailsSegue", sender: snapshot)

}

我认为这与快照有关,但我无法弄清楚。需要专业人士的帮助

1 个答案:

答案 0 :(得分:0)

如果要在同一单元格中显示所有汽车信息,首先需要一个具有三个UILabel的单元格,其中一个用于显示所有要显示的信息,这是一个示例:

enter image description here

我想您有自己的自定义单元格类,因此您需要将标签出口拖放到类中。

现在,您必须将代码更改为cellForRowAt:这样:

if let driverRequestDictionary = snapshot.value as? [String:AnyObject] {

if let typeOfCar = driverRequestDictionary["car"] as? String {
        cell.modelLabel?.text = typeOfCar
    }

if let colorOfCar = driverRequestDictionary["color"] as? String {
        cell.colorLabel?.text = colorOfCar
    }

if let plateOfCar = driverRequestDictionary["plate"] as? String {
        cell.plateLabel?.text = plateOfCar
    }
}

如果添加else语句或使用三个操作数不存在颜色和印版,则可以使用默认值:

`if let plateOfCar = driverRequestDictionary["plate"] as? String {
        cell.plateLabel?.text = plateOfCar
    }else{
       cell.plateLabel?.text = "Unknown"
    }`
  

ADVICE: 避免向cellForRowAt:发送请求,而不是使用闭包发出异步请求(例如,在viewDidLoad上)。然后使用闭包的结果填充数组,然后重新加载表视图。

编辑:

您应该以这种方式构造节点,其中giuseppesapienza是用户ID,而note是您的汽车objext:

enter image description here

相关问题