如何将表VIew Controller中的firebase数据检索传递给View Controller

时间:2017-11-12 17:28:49

标签: swift uitableview xcode9

我正在建造一辆共享IOS App原型的汽车。我的应用程序链接到firebase,我可以在表视图中检索和显示行程数据,但是当尝试在另一个视图控制器中传递数据时,数据不会显示。下面是我的表View控制器和视图控制器源代码。

导入UIKit 导入Firebase

类ViewController:UIViewController,UITableViewDelegate,UITableViewDataSource {

var myIndex = 0
var journeyList = [journeyModel]()

public func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    return journeyList.count

}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
    return 150
}


// defining firebase reference var
var refjourney: DatabaseReference!

@IBOutlet weak var journeyTable: UITableView!

public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "searchCell", for: indexPath as IndexPath) as! journeySearchTableViewCell

    var journe: journeyModel

    journe = journeyList[indexPath.row]
    print(journe.start!, journe.destination!, journe.date!, journe.driverName!)
    cell.driverNameLabel.text = journe.driverName
    cell.startLabel.text = journe.start
    cell.destinationLabel.text = journe.destination
    cell.dateLabel.text = journe.date

    return cell

}

此时,应用程序正常运行仅在将数据传递到另一个视图控制器时面临问题

override func viewDidLoad() {
    super.viewDidLoad()

    Database.database().reference().child("Journey").observe(.value, with: { (snapshot) in
        if snapshot.childrenCount > 0 {
            self.journeyList.removeAll()

            for journey in snapshot.children.allObjects as! [DataSnapshot]  {

                let journeyObject = journey.value as? [String: AnyObject]
                let start = journeyObject?["startingPoint"]
                let destination = journeyObject?["destinationPoint"]
                let driverName = journeyObject?["driverName"]
                let date = journeyObject?["tripDate"]
                let id = journeyObject?["id"]

                let journey = journeyModel(destination: destination as! String?, driverName: driverName as! String?, start: start as! String?, date: date as! String?, uid: id as! String?)

                self.journeyList.append(journey)
            }
            self.journeyTable.reloadData()
        }
    })
}

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

    performSegue(withIdentifier: "logged", sender: self)
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    var bsVC: bookSetViewController = segue.destination as! bookSetViewController



}

@IBAction func backButton(_ sender: Any) {
    dismiss(animated: true, completion: nil)
}

}

导入UIKit

class bookSetViewController:UIViewController {

var getStart = String()
var getStop = String()
var getDate = String()
var getDriver = String()

@IBOutlet weak var startingLabel: UILabel!
@IBOutlet weak var stopingLabel: UILabel!
@IBOutlet weak var daterLabel: UILabel!
@IBOutlet weak var driveLabel: UILabel!



override func viewDidLoad() {
    super.viewDidLoad()

    startingLabel.text! = getStart
    stopingLabel.text! = getStop
    daterLabel.text! = getDate
    driveLabel.text! = getDriver

}

}

导入UIKit

class journeySearchTableViewCell:UITableViewCell {

@IBOutlet weak var startLabel: UILabel!
@IBOutlet weak var destinationLabel: UILabel!
@IBOutlet weak var dateLabel: UILabel!
@IBOutlet weak var driverNameLabel: UILabel!

}

导入UIKit

class journeyModel:NSObject {

var driverName: String?
var start: String?
var destination: String?
var date: String?
var uid: String?


init(destination: String?, driverName: String?, start: String?, date: String?, uid: String?) {

    self.driverName = driverName
    self.start = start
    self.destination = destination
    self.date = date
    self.uid = uid

}

}

1 个答案:

答案 0 :(得分:1)

首先,首先 - 不要分享整个项目,只需要分享所需的内容。

准备的全部要点(对于segue ...来说是获取新控制器的句柄,并指定你需要传递的值。

您需要跟踪您感兴趣的旅程。有很多方法可以做到这一点,但最简单的方法可能是扩展您在点击行上的操作

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) 
{
    // assume you have defined journeySelected as a class-level instance of journeyModel
    journeySelected = journeyModel[indexPath.row]
    performSegue(withIdentifier: "logged", sender: self)
}

然后

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
var bsVC: bookSetViewController = segue.destination as! bookSetViewController

    bsVC.getStart = journeySelected.start
    // and for all the other fields
}