• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何使用 Firebase 数据库在 Xcode/Swift 的 TableView 中显示多个路由/子节点

[复制链接]
菜鸟教程小白 发表于 2022-12-11 22:25:10 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

这是我的 Firebase 数据库:

enter image description here

这是我的代码:

class AdminSearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var ref: DatabaseReference!
    var jobList = [jobModel2]()

    @IBOutlet weak var tlb2: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "cell2", for: indexPath) as! ViewController2TableViewCell
        let job: jobModel2
        job = jobList[indexPath.row]
        cell.ship.text = job.consignee
        cell.reference.text = job.reference
        return cell
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()
        if FirebaseApp.app() == nil {
            FirebaseApp.configure()
        }
        Database.database().reference().child("jobs").observe(DataEventType.value) { (snapshot) in
            if snapshot.childrenCount>0 {
                self.jobList.removeAll()
                for jobs in snapshot.children.allObjects as! [DataSnapshot]{
                    let jobObject = jobs.value as? [String: AnyObject]
                    let jobConsignee = jobObject?["consignee"]
                    let jobReference = jobObject?["reference"]
                    let job = jobModel2( consignee: jobConsignee as! String?,
                                         reference: jobReference as! String?
                    )
                    self.jobList.append(job)
                }
                self.tlb2.reloadData()
            }
        }
    } 
}

我面临的问题是它们在我的 TableView 中没有出现,我认为这与 .childs 有关,因为我认为我需要将其从“工作”更改为某些东西,但我想不通出去。

我想显示来自所有 UID 的所有引用的列表,而不仅仅是一个。

打印中显示的代码:

func readJobs() {


    let ref = Database.database().reference()

   let ref1 = ref.child("jobs").queryOrderedByKey()


        ref1.observeSingleEvent(of: .value, with: { snapshot in
            let allUsersAndJobs = snapshot.children.allObjects as! [DataSnapshot]
            for user in allUsersAndJobs {
                let uid = user.key
                print("user id: \(uid)")
                let thisUsersJobs = user.children.allObjects as! [DataSnapshot]
                for job in thisUsersJobs {
                    let jobKey = job.key
                    let consignee = job.childSnapshot(forPath: "consignee").value as! String
                    print(" job #: \(jobKey)")
                    print("   consignee: \(consignee)")
                }
            }
        })
    }



Best Answer-推荐答案


所以这行不通 - firebase 结构与您尝试读取的内容不匹配。

你的结构是

jobs
   uid_0
      job_key_0
          ...
      job_key_1
          ...
   uid_1
      job_key_0
          ...

并且您正在阅读包含直接子代的作业节点,但您不是在阅读子代的子代。

uid_0
uid_1
etc

因此,Firebase 结构比您正在阅读的内容更深,因此您需要在代码中再往下阅读一层才能到达作业子节点。

假设 5hs... 和 7py... 是用户 ID,这里是读取每个节点的代码,打印用户 uid,然后是他们的作业

func readJobs() {
    let ref = self.ref.child("jobs")
    ref.observeSingleEvent(of: .value, with: { snapshot in
        let allUsersAndJobs = snapshot.children.allObjects as! [DataSnapshot]
        for user in allUsersAndJobs {
            let uid = user.key
            print("user id: \(uid)")
            let thisUsersJobs = user.children.allObjects as! [DataSnapshot]
            for job in thisUsersJobs {
                let jobKey = job.key
                let consignee = job.childSnapshot(forPath: "consignee").value as! String
                print(" job #: \(jobKey)")
                print("   consignee: \(consignee)")
            }
        }
    })
}

和输出

user id: 5HS
 job #: job_0
   consignee: Anni
 job #: job_1
   consignee: Ralph
user id: 7py
 job #: job_0
   consignee: Larry
 job #: job_1
   consignee: Bill

关于ios - 如何使用 Firebase 数据库在 Xcode/Swift 的 TableView 中显示多个路由/子节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55061509/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap