2017-08-10 114 views
0

这是我的数据库结构。我将每个具有uuid的事件作为其唯一标识符存储在根节点“events”下。Swift - 如何从Firebase中检索多个值并在UITABLEVIEW上显示它们

events: 
    uuid: 
     name: summer festival 2017 
     site: vegas 
     participants: 
      employeeID1: true 
      employeeID2: true 

我有一个有3列一个UITableView,我想这个表格中显示以下数据

Colum1(event name)  Column2(site)   Column2(# of Participants) 
summer festival 2017  vegas     2000 

我试过的代码块,但它不会工作。请帮忙。

let myRef = ref?.child("events") 

myRef?.queryOrdered(byChild: "name").observe(.childAdded, with: { (snapshot) in 

      for child in snapshot.children { 
       let snap = child as! DataSnapshot 
       if let eventName = snap.value["name"] as String { 
        self.events.append(eventName) 
       } 
      } 

     }) 
+0

? –

+0

你好。感谢您的回复。是的,是否有可能,如果是的话,我该怎么做? – TSM

+0

请不要利用查询,如果你想要所有的数据一次只是获得数据库处理程序中的所有事件,并稍后对它进行排序我在我的聊天应用程序中应该向我展示一些Demo吗? –

回答

0

这里是您可以尝试的代码。希望能帮助到你!

//TableView outlet 
@IBOutlet weak var namesTable: UITableView! 

//Array to save all event Names 
var names = [String]() 

//Database handler Object 
var readData = DatabaseReference() 

//I use a separate Dictionary to take snapshot out of handler if required 
var postData = [String:AnyObject]() 

//make use of serrate function or calling didLoad 
override func viewDidLoad() { 
    super.viewDidLoad() 

//Database reference to Fetch Data 
readData = Database.database().reference() 

//here is a databaseHandler which fetch all events data at once 
    self.databaseHandler = self.readData.child("events").observe(.value, with: { (snapshot) in 
     self.postData = ((snapshot.value) as? [String : AnyObject]!)! 

     //print data to see if you require 
     //print it to generate loop for required things 
     print(self.postData) 

     //make snapshot as [string:anyobject] 
     if let snapDict = snapshot.value as? [String:AnyObject]{ 

      ////here is a loop which will run after you get your snapshot 
      for each in snapDict{ 
       //you will get all your names in a array all at once 
       let artistName = each.value["name"] as! String 

       //use this array to save all event name 
       self.names.append(artistName) 

      } 
      //remove ypur datasource and delegate from Storyboard 
      //here you can provide datasource and delegate 
      //if on same controller you have multiple functionality 
      //write reload table line here with datasource and delegate 
      self.namesTable.dataSource = self 
      self.namesTable.delegate = self 
     } 
    }) 
} 

的TableView代表

func numberOfSectionsInTableView(tableView: UITableView) -> Int { 
    return 1 
} 

func tableView(_ tableView:UITableView, numberOfRowsInSection section:Int) -> Int 
{ 
    return names.count 

} 

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 
{ 
    let cell = self.namesTable.dequeueReusableCell(withIdentifier: "Cell")! 
    cell.textLabel?.text = names[indexPath.row] 
    return cell 
} 
你想获得的所有数据在数据库处理器和显示器中的一个机制TableView中一次全部
+0

如果仍然遇到任何问题,请让我知道 –

+0

谢谢iOS Geek。我会试试这个 – TSM

+0

好吧我在等待让我知道任何问题 –

相关问题