2016-09-28 116 views
0

我試圖將我的數據從Firebase數據庫轉換爲Swift中的特定標籤。我在TableView(如Main.storyboard)中有兩個標籤12Swift:從Firebase數據庫檢索數據以標記

ViewController,我有這樣的代碼:

import UIKit 
import Firebase 
import FirebaseDatabase 

struct confStruct { 
    let title : String! 
    let place : String! 
} 

class EVS_Table_VC: UITableViewController { 
    var conf = [confStruct]() 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let databaseRef = FIRDatabase.database().reference() 
     databaseRef.child("conferences").queryOrderedByKey().observeEventType(.ChildAdded, withBlock: { 
        snapshot in 

      self.tableView.reloadData() 
     }) 
    } 

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 

     return conf.count 
    } 

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

     let cell = tableView.dequeueReusableCellWithIdentifier("Cell") 

     let label1 = cell?.viewWithTag(1) as! UILabel 
     label1.text = conf[indexPath.row].title 

     let label2 = cell?.viewWithTag(2) as! UILabel 
     label2.text = conf[indexPath.row].place 

     return cell! 

    }  
} 

但沒有在模擬器上顯示出來。有人有一個主張如何解決這個問題嗎?這個結構初始化我的變量從 數據庫? (titleplace)。

JSON樹:

"conferences": { 
      "Key": { 
       "date": "some date" 
       "deadline": "some deadline" 
       "place": "some place" 
       "title": "some title" 
        } 
       } 
+0

你需要從**快照數據追加到** **重新加載表數據前的conf ** –

+0

給你的JSON樹.. – Dravidian

+0

@ Dravidian我編輯。 – gryzek

回答

0

更改您的結構來: -

struct confStruct { 
    let title : String! 
    let place : String! 

    init(title_String : String!, place_String : String!){ 

     self.title = title_String 
     self.place = place_String 
    } 

} 

,並且: -

FIRDatabase.database().reference().child("conferences/Key").observeSingleEvent(of: .value, with: {(snap) in 


     if let snapDict = snap.value as? [String:AnyObject]{ 

      let titleS = snapDict["title"] as! String 
      let placeS = snapDict["place"] as! String 

      let temp = confStruct.init(title_String: titleS, place_String: placeS) 
      self. conf.append(temp) 
      self.tableView.reloadData() 
     } 
    }) 
+0

它不起作用。沒有任何內容顯示在列表中。 – gryzek

+0

我做了你所說的。但我也改變了結構,以便迅速正常工作3.謝謝 – gryzek