将Firebase数据从TableViewCell传递到ViewController

时间:2017-05-29 04:22:37

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

我一直在尝试多种方法将我的firebase数据从TableViewCell传递到ViewController,并且无法找到最简单的解决方案。

我有一个带有EncounterTableViewCells的EncounterTableViewController。这是使用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()
}
}

2 个答案:

答案 0 :(得分:1)

您需要传递dictionary的对象,而不是传递Encounter,因此在名为Encounter的{​​{1}}中声明一个类型为EncounterDetailViewController的实例属性。现在selectedEncounter方法prepareForSegueEncountersTableViewController的对象从数组传递给此Encounter

detailViewController.

<强> EncounterDetailViewController

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    if segue.identifier == "segueToEncounterCard" {
        let destination = segue.destination as! EncounterDetailViewController
        if let indexPath = self.tableView.indexPathForSelectedRow {
            destination.selectedEncounter = self.encounters[indexPath.row]
        }
    }
} 

答案 1 :(得分:0)

在EncountersTableViewController中添加tableView委托方法func tableView(UITableView, didSelectRowAt: IndexPath),在此方法中,您可以使用indexPath.row从遇到数组中获取所选的遭遇。遇到遭遇后,您可以performSegue(withIdentifier: "segueToEncounterCard" , sender: encounter )

在prepareForSegue方法中,您可以通过将发件人值转换为Encounter对象来获得遭遇,并且可以将遭遇传递到目标。