【发布时间】:2018-09-26 19:36:05
【问题描述】:
您好,我正在尝试将从 Firebase 检索并显示在 tableViewCell 中的数据传递给另一个 viewController - 一个更详细的视图控制器。希望你能帮忙?
这个想法是单元格显示一个城市和国家,当点击它时,它会转到一个新的视图控制器,显示该城市的更多信息。所以进一步的信息是;城市、国家、地点、照片网址。
我已经为此花费了无数个小时,试图弄清楚在 didSelectRowAt 中编写什么代码并为 segue 做准备。
提前致谢。
数据库:
这是旅行目的地类:
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
}
}
}
}
【问题讨论】:
标签: ios uitableview firebase firebase-realtime-database segue