问题描述
我有问题显示在由segue连接的另一个视图控制器中选定单元格的构造和模型。单元格显示Vin数字,我想要将该特定vin编号的制作和模型信息传递给另一个ViewController。我试图设置传递的价值,但不能得到它的工作。有人可以看一下,看看他们能否发现我出错的地方?
以下是我的firebase数据库的样子:
车辆: {
5UXKR0C34H0X82785:{
车辆信息:{
make:toyota,
型号:corolla,
VinNumber:5UXKR0C34H0X82785
}
$ / code $ / $ p
这是我的Vehicle模型类
import Foundation
导入FirebaseDatabase
结构VehicleModel {
var Make:String?
var Model:String?
var VinNumber:String?
$ b $ 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?字符串
模型= snapshotValue [模型]为? String
$ p $这些是我的视图控制器代码
$ p $ 导入UIKit
导入Firebase
导入FirebaseDatabase
类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()
}
覆盖func tableView(_ tableView:UITableView,numberOfRowsInSection
部分:Int) - > Int {
return userList.count
}
覆盖func tableView(_ tableView:UITableView,cellForRowAt indexPath:
IndexPath) - > UITableViewCell {
//设置单元格内容
let cell = tableView.dequeueReusableCell(withIdentifier:cellId,for:
indexPath)作为UITableViewCell
let eachvehicle = userList [indexPath.row ]
cell.textLabel!.text =\(String(descriptions:eachvehicle.VinNumber))
return cell
}
func fetchUsers(){
refHandle = ref.child(Vehicles)。如果让字典= snapshot.childSnapshot(forPath:
),观察(.childAdded,在
中有{
(快照) (字典)
打印(字典)
让VinNumber = dictionary [VinNumber]
让Make = dictionary [Make]
模型=字典[模型]
self.userList.insert(VehicleModel(Make:Make as?String,Model:
Model as?String,VinNumber:VinNumber as?String) 0)
self.tableView.reloadData()
}
}}
}
}
覆盖func tableView(_ tableView:UITableView,didSelectRowAt indexPath:
IndexPath){
let storyboard = UIStoryboard :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到目前为止)
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at:indexPath)!作为UITableViewCell
makeToPass = currentCell.Model
modelToPass = currentCell.Make
}
覆盖func prepare(for segue:UIStoryboardSegue,sender:Any?){
if segue .identifier ==toMapView{
var viewController = segue.destination as!
AdditionalInfoViewController
viewController.makeToPass = makeToPass
viewController.modelToPass = modelToPass
$ b}
}
我的AdditionalInfoView控制器中的变量结构
$ b $ pre $ var $ passValue:串?
var latitudePassedValue:String?
var longitudePassedValue:String?
这个问题似乎是在解析数据从数据库。该结构包含 make
和模型
全部小写:
<$
make:toyota,
模型:corolla,
// ...
code>
但是在解析方法中,用大写的第一个字母来处理它:
// ...
Make = snapshotValue [Make] as? String //Make
Model = snapshotValue [Model] as? String //Model
// ...
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?
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"
// ...
这篇关于将Firebase数据从tableview传递给ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!