2017-09-25 57 views
0

我在从我的TableView中显示Firebase数据时遇到问题。我只希望将vin编号显示在表格视图中。现在我要么获得显示“无”的单元格,要么没有单元格。Firebase到Tableview问题

我的目标是让每个单元显示Vin数。

有人可以看一看,让我知道我有什么问题吗?

谢谢! 亚历

这里是我的火力数据库是什么样子

孩子--Vehicles

孩子 - 5UXKR0C34H0X82785

child-- VehicleInfo 然后在 “车辆信息” 的孩子则显示这些三个字段

品牌:“丰田”

型号:“coro LLA”

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.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()    
     } 
    }) 
    } 
} 

而且,我有显示化妆的问题以及由segue连接的另一个视图控制器中所选单元的模型。我试图设置传递值,但无法使其工作。

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 = makeValueToPass 
     viewController.modelToPass = modelValueToPass    

    } 
} 

回答

1

纠正我,如果我错了,但这是你的数据结构,对不对?

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

才能访问VehicleInfo下的数据这意味着,你需要指定位置。有几种方法可以做到这一点,但其中一个将使用childSnapshot(forPath:)

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()    
     } 
    }) 
    } 
} 
+0

谢谢仁!这工作!你太棒了!!!!!!! – Alex

+0

很高兴帮助!另外,请记住,如果除'VehicleInfo'之外的'Vehicles'下的推式Ids下还有其他数据,那么无论您是否使用除VehicleInfo之外的数据,每当添加新车时,都会下载该数据。 。如果除了'VehicleInfo'之外还有其他数据,比如孩子'5UXKR0C34H0X82785',那么您可能需要重新考虑如何触发侦听器或数据库的结构,以便您只需下载所需的数据。 –

+0

谢谢你提到这一点。我一定会查看数据库,看看我是否可以重组它。再次感谢!!! – Alex