Passing Firebase Data from tableview to ViewController

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-13 03:39:45

问题


I am having issues displaying the make and model of the selected cell in another view controller connected by a segue. The Cells display the Vin Number and I want the make and model info for that specific vin number passed to another ViewController. I have attempted to set up passing the values but can not get it to work. Can someone take a look and see if they can spot where I went wrong?

Here is what my firebase database looks like:

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

Here is my Vehicle model class

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
  }
}

Here is my view controller code

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            

    }
}

structure for variables in my AdditionalInfoView Controller

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

回答1:


The problem seems to be in the parsing of the data from the database. The structure has make and model all lower case:

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

But in the parsing method you're addressing it with the first letter in uppercase:

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


来源:https://stackoverflow.com/questions/46475879/passing-firebase-data-from-tableview-to-viewcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!