将Firebase数据从TableViewCell传递到另一个ViewController

时间:2018-04-16 23:59:48

标签: ios uitableview firebase firebase-realtime-database segue

您好我正在尝试将从Firebase检索到的数据传递到tableViewCell并将其显示到另一个viewController - 一个更详细的视图控制器。希望你能帮忙吗?

这个想法是Cell显示一个城市和国家,当点击它时,它会转到一个新的视图控制器,显示该城市的更多信息。所以进一步的信息是;城市,国家,位置,photoUrl。

我已经无数个小时试图弄清楚在didSelectRowAt和segue准备中要编码的内容。

提前致谢。

数据库:

enter image description here

这是旅行目的地类:

import Foundation
import FirebaseDatabase

class tripDestinations {

var country: String?
var city: String?
var location: String?
var photoUrl: String?

init(cityText: String?, countryText: String?, locationText: String?, photoUrlString: String?) {
    self.city = cityText
    self.country = countryText
    self.location = locationText
    self.photoUrl = photoUrlString
}

init(snapshot: DataSnapshot) {
    let snapshotValue = snapshot.value as! [String: AnyObject]
    city = snapshotValue["city"] as? String
    country = snapshotValue["country"] as? String
    location = snapshotValue["location"] as? String
    photoUrl = snapshotValue["photoUrl"] as? String
}  

这是第一个tableViewController:

import UIKit
import Firebase
import FirebaseDatabase

class HomeTableViewController: UITableViewController {

    var trips = [tripDestinations]()
    var ref: DatabaseReference!
    var refHandle: UInt!

    override func viewDidLoad() {
        super.viewDidLoad()

        ref = Database.database().reference()

        tableView.delegate = self
        tableView.dataSource = self

        navigationController?.navigationBar.prefersLargeTitles = true

        loadTrips()
    }

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

    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cellId", for: indexPath) as UITableViewCell

        let cityLabel = cell.viewWithTag(1) as! UILabel
        cityLabel.text = trips[indexPath.row].city

        let countryLabel = cell.viewWithTag(2) as! UILabel
        countryLabel.text = trips[indexPath.row].country

        return cell
    }

    func loadTrips() {
        refHandle = ref.child("destinations").queryOrderedByKey().observe(.childAdded, with: { (snapshot) in
            if let dictionary = snapshot.value as? [String: AnyObject] {
                print(dictionary)

                let city = dictionary["city"]
                let country = dictionary["country"]
                let location = dictionary["location"]
                let photoUrl = dictionary["photoUrl"]

                self.trips.insert(tripDestinations(cityText: city as? String, countryText: country as? String, locationText: location as? String, photoUrlString: photoUrl as? String), at: 0)

                self.tableView.reloadData()
            }
        })
    }

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

    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "showDetail" {
            if let indexPath = tableView.indexPathForSelectedRow {
                let destinationController = segue.destination as! DetailTableViewController
            }
        }
    }
}

2 个答案:

答案 0 :(得分:0)

在您的didselectRowat调用performsegue函数中,如下所示。

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        performSegue(withIdentifier: "showDetail", sender: self)
    }

在DetailTableViewController中创建数组

var tripsData = [tripDestinations]()

在performsegue函数中添加这些行

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "showDetail" {

        if let indexPath = tableView.indexPathForSelectedRow {
            let destinationController = segue.destination as! DetailTableViewController

            destinationController.tripsData = trips[indexPath?.row]
        }
    }
}

答案 1 :(得分:0)

在详细视图控制器中添加以下代码行

var destination : tripDestinations? {
            didSet {
                print(destination?.country!)
            }
        }

在didSelectRowAt方法

中添加以下行
performSegue(withIdentifier: "showDetail", sender: self)

添加以下行准备segue方法并尝试

destinationController.destination = self.trips[indexPath.row]