ios - Swift - 如何从 Firebase 检索多个值并将它们显示在 UITABLEVIEW 上

标签 ios arrays swift uitableview firebase

这是我的数据库的结构。我将每个事件以 uuid 作为其唯一标识符存储在根节点“events”下。

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

        })

最佳答案

这是您可以尝试的代码。希望有帮助!

//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 委托(delegate)

 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
}

关于ios - Swift - 如何从 Firebase 检索多个值并将它们显示在 UITABLEVIEW 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45604227/

相关文章:

objective-c - 如何存储类变量,然后在 Objective-C 中调用该类的静态方法

Java长度方法错误

arrays - 在ng-repeat中按具有整数值的属性过滤对象数组

ios - 如何在不使用 UIWebView 的情况下在 iOS 中显示动画 svg 文件

ios - 在 Windows 机器上压缩后的预期标识符或 '('

ios - 如何组合 2 个键以实现可本地化

objective-c - 在 Objective-C 和 iOS 中,似乎委托(delegate)可以去任何方向?

iphone - 为什么 NSMutableArray 没有显示在 UITableView 中?

javascript - mongodb 用变量更新数组

ios - 在 Swift 中,如何等待一个动画在下一个动画开始之前完成?