将 Firebase 数据从 TableViewCell 传递到另一个 ViewController
Passing Firebase data from TableViewCell to another ViewController
您好,我正在尝试将从 Firebase 检索并显示在 tableViewCell 中的数据传递给另一个 viewController - 一个更详细的视图控制器。希望你能帮忙?
这个想法是 Cell 显示一个城市和国家,当点击它时它会转到一个新的视图控制器,显示该城市的更多信息。所以进一步的信息是;城市、国家、位置、照片网址。
我已经为此花费了无数个小时,试图找出在 didSelectRowAt 中编码的内容并为 segue 做准备。
提前致谢。
数据库:
这是旅行目的地class:
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
}
}
}
}
在您的 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]
}
}
}
在详细视图控制器中添加以下代码行
var destination : tripDestinations? {
didSet {
print(destination?.country!)
}
}
在 didSelectRowAt 方法中添加下面一行
performSegue(withIdentifier: "showDetail", sender: self)
添加以下行 Prepare for segue method and try
destinationController.destination = self.trips[indexPath.row]
您好,我正在尝试将从 Firebase 检索并显示在 tableViewCell 中的数据传递给另一个 viewController - 一个更详细的视图控制器。希望你能帮忙?
这个想法是 Cell 显示一个城市和国家,当点击它时它会转到一个新的视图控制器,显示该城市的更多信息。所以进一步的信息是;城市、国家、位置、照片网址。
我已经为此花费了无数个小时,试图找出在 didSelectRowAt 中编码的内容并为 segue 做准备。
提前致谢。
数据库:
这是旅行目的地class:
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
}
}
}
}
在您的 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]
}
}
}
在详细视图控制器中添加以下代码行
var destination : tripDestinations? {
didSet {
print(destination?.country!)
}
}
在 didSelectRowAt 方法中添加下面一行
performSegue(withIdentifier: "showDetail", sender: self)
添加以下行 Prepare for segue method and try
destinationController.destination = self.trips[indexPath.row]