【问题标题】:Taking all of the child keys in a section of my Firebase Database and displaying on a tableview在我的 Firebase 数据库的一部分中获取所有子键并显示在 tableview 上
【发布时间】:2017-07-29 09:27:44
【问题描述】:

我正在尝试从我的 firebase 数据库中的子键列表创建一个数组。我可以提取数据,将其插入数组,然后将其打印到控制台,但它不会显示在我的表格视图中。我已确保 tableview 已连接到控制器并且单元格匹配。这是我的代码和数据库结构。

import UIKit
import Firebase
import FirebaseDatabase

class TeamDataSegueViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {

    @IBOutlet weak var ageListTable: UITableView!

    let ref = FIRDatabase.database().reference()
    var ageList = [String]()

    override func viewDidLoad() {
        super.viewDidLoad()

        dataObserver()

        self.ageListTable.delegate = self
        self.ageListTable.dataSource = self
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func dataObserver() {
        ref.child("Database").observeSingleEvent(of: .value, with: { (snapshot) in

            for child in snapshot.children {
                let snap = child as! FIRDataSnapshot
                self.ageList.append(snap.key)
                print(self.ageList)
                print(snap.key)
            }
        })
    }


    func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Potentially incomplete method implementation.
        // Return the number of sections.
        return 1
    }

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete method implementation.
        // Return the number of rows in the section.
        return ageList.count
    }


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = ageListTable.dequeueReusableCell(withIdentifier: "cell")

        cell?.textLabel?.text = ageList[indexPath.row]

        return cell!
    }
}

我不知道如何展示它,但差不多就是这样。我希望能够获取所有年龄组并在表格视图中列出它们,即age_group1、age_group2。

 - 504
     - Database
         - age_group1
             - count
         - age_group2
             - count

【问题讨论】:

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


    【解决方案1】:

    你需要在完成数组填充后重新加载表的数据,即

    func dataObserver() {
        ref.child("Database").observeSingleEvent(of: .value, with: { (snapshot) in
    
            for child in snapshot.children {
                let snap = child as! FIRDataSnapshot
                self.ageList.append(snap.key)
                print(self.ageList)
                print(snap.key)
                self.ageListTable.reloadData()
            }
        })
    }
    

    【讨论】:

    • 非常感谢,我知道在我能够正确提取数据并将其显示在控制台中之前,我曾经拥有它,但是当我试图从头开始重做它时,它一定已经被删除了。
    【解决方案2】:

    执行此操作的最简单方法是将每个快照转换为字典,然后提取值,如下所示:

        for child in snapshot.children {
                let snap = child as! FIRDataSnapshot
    
                if let childDic = FIRSnap.value as? Dictionary<String, AnyObject> {
                    //do something with this dictionary
                }
    
                self.ageList.append(snap.key)
                print(self.ageList)
                print(snap.key)
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 1970-01-01
      • 2018-01-28
      • 1970-01-01
      • 2017-11-13
      相关资源
      最近更新 更多