将Firebase数据从tableview传递到ViewController

时间:2017-09-28 18:20:10

标签: uitableview firebase firebase-realtime-database segue

我在通过segue连接的另一个视图控制器中显示所选单元格的品牌和型号时遇到问题。单元格显示Vin编号,我想要传递给另一个ViewController的特定vin编号的品牌和型号信息。我试图设置传递值但无法使其工作。有人可以看一看,看看他们是否能发现我哪里出错了?

以下是我的firebase数据库的样子:

Vehicles: {
 5UXKR0C34H0X82785: {
    VehicleInfo: {
        make:"toyota",
        model:"corolla",
        VinNumber: "5UXKR0C34H0X82785"
      }
   }
}

这是我的车辆模型类

import Foundation
import FirebaseDatabase

struct VehicleModel {
var Make: String?
var Model: String?
var VinNumber: String?

init(Make: String?, Model: String?, VinNumber: String?){
    self.Make = Make
    self.Model = Model
    self.VinNumber = VinNumber
}

    init(snapshot: DataSnapshot) {
    let snapshotValue = snapshot.value as! [String: AnyObject]
    VinNumber = snapshotValue["VinNumber"] as? String
    Make = snapshotValue["Make"] as? String
    Model = snapshotValue["Model"] as? String
  }
}

这是我的视图控制器代码

import UIKit
import Firebase
import FirebaseDatabase
class InventoryTableViewController: UITableViewController{


    var ref: DatabaseReference!
    var refHandle: UInt!
    var userList = [VehicleModel]()

    let cellId = "cellId"

    override func viewDidLoad() {
    super.viewDidLoad()

    ref = Database.database().reference()

    tableView.delegate = self
    tableView.dataSource = self

    tableView?.register(UITableViewCell.self, forCellReuseIdentifier: 
    "cellId")
    fetchUsers()
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection 
section: Int) -> Int {
    return userList.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: 
IndexPath) -> UITableViewCell {
    // Set cell contents
    let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: 
indexPath) as UITableViewCell
    let eachvehicle = userList[indexPath.row]
    cell.textLabel!.text = "\(String(describing: eachvehicle.VinNumber))"
    return cell
}

func fetchUsers(){
            refHandle = ref.child("Vehicles").observe(.childAdded, with: { 
(snapshot) in
             if let dictionary = snapshot.childSnapshot(forPath: 
"VehicleInfo").value as? [String: AnyObject] {
            print(dictionary)
            let VinNumber = dictionary["VinNumber"]
            let Make = dictionary["Make"]
            let Model = dictionary["Model"]
            self.userList.insert(VehicleModel(Make: Make as? String, Model: 
Model as? String, VinNumber:         VinNumber as? String), at: 0)
            self.tableView.reloadData()              
        }
    })
  }
}  

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: 
IndexPath) {
    let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main)
    let destination = storyboard.instantiateViewController(withIdentifier: 
"AdditionalInfoViewController") as! AdditionalInfoViewController
    navigationController?.pushViewController(destination, animated: true)
    performSegue(withIdentifier: "toAdditionalInfo", sender: self)
        let row = indexPath.row
        print("working so far ")

    let indexPath = tableView.indexPathForSelectedRow!
    let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell
    makeToPass = currentCell.Model
    modelToPass = currentCell.Make      
} 
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "toMapView" {
        var viewController = segue.destination as! 
AdditionalInfoViewController
        viewController.makeToPass = makeToPass
        viewController.modelToPass = modelToPass            

    }
}

我的AdditionalInfoView控制器中的变量结构

    var passedValue: String?
    var latitudePassedValue: String?
    var longitudePassedValue: String?

1 个答案:

答案 0 :(得分:0)

问题似乎在于解析数据库中的数据。该结构的makemodel全部为小写:

// ...
make:"toyota",
model:"corolla",
// ...

但是在解析方法中,你用大写的第一个字母来解决它:

// ...
Make = snapshotValue["Make"] as? String // "Make"
Model = snapshotValue["Model"] as? String // "Model"
// ...