2017-05-29 96 views
0

我一直在嘗試多種方式將我的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() 
} 
} 
+0

是SEGUE從的UITableViewCell連接到destinationController? –

+0

如何從EncountersTableViewController導航到EncounterDetailViewController? – nayem

+0

Segue從EncounterTableViewCell連接到EncounterDetailViewController – pmanning

回答

1

不是傳遞dictionary你需要傳遞的Encounter對象,所以在你的EncounterDetailViewController名爲selectedEncounter聲明Encounter類的一個實例屬性。現在在EncountersTableViewControllerprepareForSegue方法傳遞的Encounter對象從陣列到這個detailViewController.

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] 
     } 
    } 
} 

EncounterDetailViewController

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 selectedEncounter: Encounter? 

    // MARK: - View did load 
    override func viewDidLoad() { 
     super.viewDidLoad() 
     //access selectedEncounter property here and set all label's text 
    } 
} 
+0

Ahh感謝info @Nirav!試圖訪問字典信息。知道必須有一個更簡單的方法。 – pmanning

+0

@pmanning歡迎隊友:),我忘了說的另外一件事是,不要在Swift中使用NSArray和NSDictionary使用本地類型數組和字典意味着你可以像這樣檢索媒體數組restDict [「media 「] as! [[String:Any]]'現在不需要將第一個對象轉換爲字典,因爲我們將數組指定爲字典數組。 –

+0

謝謝@Nirav實際上有助於我檢索一些嵌套數據的下一個任務:) – pmanning

0

在EncountersTableViewController添加的tableView委託方法,func tableView(UITableView, didSelectRowAt: IndexPath)並且此方法內,可以通過使用indexPath得到遭遇陣列所選遭遇。行。遇到相遇後,您可以performSegue(withIdentifier: "segueToEncounterCard" , sender: encounter)

而在prepareForSegue方法中,您可以通過將發件人值強制轉換爲Encounter對象來獲得相遇,並且可以將相遇傳遞給目標。

相關問題