我一直在嘗試多種方式將我的Firebase數據從TableViewCell傳遞到ViewController,但無法找出最簡單的解決方案。將Firebase數據從TableViewCell傳遞到ViewController
我有一個EncounterTableViewController與EncounterTableViewCells。這是由Firebase數據填充的。當選擇EncounterTableViewCell時,我想將數據傳遞給EncounterDetailViewController。
EncountersTableViewController.swift
class EncountersTableViewController: UITableViewController {
var encounters : [Encounter] = []
// MARK: - View Did load
override func viewDidLoad() {
super.viewDidLoad()
showAllEncounters()
}
// MARK: - Encounter filters
func showAllEncounters() {
// Firebase tableview data
FIRDatabase.database().reference().child("encounters").observeSingleEvent(of: .value, with: { (snapshot) in
for rest in snapshot.children.allObjects as! [FIRDataSnapshot] {
guard let restDict = rest.value as? [String: Any] else { continue }
let encounter = Encounter()
encounter.sharkName = (restDict["shark_name"] as? String)!
encounter.date = (restDict["trip_date"] as? String)!
encounter.contributorName = (restDict["contributor"] as? String)!
encounter.contributorImage = (restDict["contributor_image"] as? String)!
let mediaDict = (restDict["media"] as? NSArray)
let firstImage = mediaDict![0] as! NSDictionary
encounter.mainImage = firstImage["thumb_url"] as! String
self.encounters.append(encounter)
self.tableView.reloadData()
}
})
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segueToEncounterCard" {
let destination = segue.destination as! EncounterDetailViewController
}
}
EncounterDetailViewController.swift
class EncounterDetailViewController: UIViewController {
// MARK: - Outlets
@IBOutlet weak var encounterDateLabel: UILabel!
@IBOutlet weak var locationLabel: UILabel!
@IBOutlet weak var lengthLabel: UILabel!
@IBOutlet weak var contributorNameLabel: UILabel!
@IBOutlet weak var contributorImageView: UIImageView!
// MARK: - Properties
var dictionary: [String:AnyObject]?
// MARK: - View did load
override func viewDidLoad() {
super.viewDidLoad()
}
}
是SEGUE從的UITableViewCell連接到destinationController? –
如何從EncountersTableViewController導航到EncounterDetailViewController? – nayem
Segue從EncounterTableViewCell連接到EncounterDetailViewController – pmanning