菜鸟教程小白 发表于 2022-12-11 22:25:10

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


                                            <p><p>这是我的 Firebase 数据库:</p>

<p> <a href="/image/6M6jO.png" rel="noreferrer noopener nofollow"><img src="/image/6M6jO.png" alt="enter image description here"/></a> </p>

<p>这是我的代码:</p>

<pre><code>class AdminSearchViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
    var ref: DatabaseReference!
    var jobList = ()

    @IBOutlet weak var tlb2: UITableView!

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
      let cell = tableView.dequeueReusableCell(withIdentifier: &#34;cell2&#34;, for: indexPath) as! ViewController2TableViewCell
      let job: jobModel2
      job = jobList
      cell.ship.text = job.consignee
      cell.reference.text = job.reference
      return cell
    }

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

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

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

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

<p>打印中显示的代码:</p>

<pre><code>func readJobs() {


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

   let ref1 = ref.child(&#34;jobs&#34;).queryOrderedByKey()


      ref1.observeSingleEvent(of: .value, with: { snapshot in
            let allUsersAndJobs = snapshot.children.allObjects as!
            for user in allUsersAndJobs {
                let uid = user.key
                print(&#34;user id: \(uid)&#34;)
                let thisUsersJobs = user.children.allObjects as!
                for job in thisUsersJobs {
                  let jobKey = job.key
                  let consignee = job.childSnapshot(forPath: &#34;consignee&#34;).value as! String
                  print(&#34; job #: \(jobKey)&#34;)
                  print(&#34;   consignee: \(consignee)&#34;)
                }
            }
      })
    }
</code></pre></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>所以这行不通 - firebase 结构与您尝试读取的内容不匹配。</p>

<p>你的结构是</p>

<pre><code>jobs
   uid_0
      job_key_0
          ...
      job_key_1
          ...
   uid_1
      job_key_0
          ...
</code></pre>

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

<pre><code>uid_0
uid_1
etc
</code></pre>

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

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

<pre><code>func readJobs() {
    let ref = self.ref.child(&#34;jobs&#34;)
    ref.observeSingleEvent(of: .value, with: { snapshot in
      let allUsersAndJobs = snapshot.children.allObjects as!
      for user in allUsersAndJobs {
            let uid = user.key
            print(&#34;user id: \(uid)&#34;)
            let thisUsersJobs = user.children.allObjects as!
            for job in thisUsersJobs {
                let jobKey = job.key
                let consignee = job.childSnapshot(forPath: &#34;consignee&#34;).value as! String
                print(&#34; job #: \(jobKey)&#34;)
                print(&#34;   consignee: \(consignee)&#34;)
            }
      }
    })
}
</code></pre>

<p>和输出</p>

<pre><code>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
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何使用 Firebase 数据库在 Xcode/Swift 的 TableView 中显示多个路由/子节点,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/55061509/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/55061509/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何使用 Firebase 数据库在 Xcode/Swift 的 TableView 中显示多个路由/子节点