菜鸟教程小白 发表于 2022-12-11 20:45:55

ios - 如何在表格 View 中添加 Firebase 值


                                            <p><p>对于如何将表格 View 与 Firebase 上的值连接起来,我自己有一个问题,或者更确切地说是一个误解。</p>

<p>我的信息:</p>

<p>我有一个 Firebase,许多其他文件夹之一称为“Flights”。在我的应用程序 View 中,我可以按照以下路径在此文件夹中添加一些信息:Flights > userId > autoId > 我感兴趣的值,包括日期和其他一些字符串</p>

<p>我的问题是:</p>

<p>如何在 tableView 中添加,当这个是由 viewDidLoad 创建时,我的文件夹中的每个 autoId 一个新的个性化单元格 Flights > userId ?</p>

<p>我的尝试:</p>

<p>我声明我的数组:</p>

<pre><code> var datas: = []
</code></pre>

<p>我调用 viewDidLoad() 这个函数:</p>

<pre><code>func loadFlights() {
    ref = Database.database().reference()
   let userID = Auth.auth().currentUser?.uid
    ref.child(&#34;flights&#34;).child(userID!).childByAutoId().queryOrderedByKey().observe(.childAdded, with: { (snapshot) in

      if let valueDictionary = snapshot.value as?
      {

            let date = valueDictionary[&#34;Date&#34;]
            let type = valueDictionary[&#34;aircraft-model&#34;]
            let registration = valueDictionary[&#34;aircraft-registration&#34;]

            let totalTime = valueDictionary[&#34;TOTAL-TIME&#34;]

            let depTime = valueDictionary[&#34;departure-time&#34;]
            let depPlace = valueDictionary[&#34;departure-place&#34;]

            let arrTime = valueDictionary[&#34;arrival-time&#34;]
            let arrPlace = valueDictionary[&#34;arrival-place&#34;]
            self.datas.append(Flight(from: date ?? &#34;XX-XX-XX&#34;, type!, registration!, totalTime!, depTime!, depPlace ?? &#34;NIL&#34;, arrTime!, arrPlace!))

            self.tableView.reloadData()
      }else{
            // nothing need to happens
      }
    })

}
</code></pre>

<p>最后我有我的表管理器部分:</p>

<pre><code>// MARK: - Table view data source

override func numberOfSections(in tableView: UITableView) -&gt; Int {
    // #warning Incomplete implementation, return the number of sections
    return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -&gt; Int {
    // #warning Incomplete implementation, return the number of rows
    return datas.count
    //return newFlight.count
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -&gt; UITableViewCell {
    // creer la cellule

    let cell = tableView.dequeueReusableCell(withIdentifier: &#34;lbCells&#34;) as! LogBookCell

    cell.dateLabel.text = datas.date
    cell.typeLabel.text = datas.type
    cell.RegistrationLabel.text = datas.regi

    cell.totalTimeLabel.text = datas.totalTime

    cell.depTimeLabel.text = datas.depTime
    cell.depPlaceLabel.text = datas.depPlace

    cell.arrTimeLabel.text = datas.arrTime
    cell.arrPlaceLabel.text = datas.arrPlace

    return cell
}

override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -&gt; Bool {
    return true
}

override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCell.EditingStyle, forRowAt indexPath: IndexPath) {
    if editingStyle == .delete{
      datas.remove(at: indexPath.row)
      tableView.reloadData()
    }
}
</code></pre>

<p>我不明白为什么当我在我的应用程序上加载 tableView 页面时什么都没有出现...</p>

<p>感谢您的帮助!</p>

<p>Flyer-74</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以在添加或删除时观察一个值。</p>

<pre><code>var ref: DatabaseReference! = Database.database().reference()
   ref.child(&#34;devices&#34;).child(user!.uid).child(&#34;device_name&#34;).setValue(&#34;iPhone test&#34;)

let newRef = ref.child(&#34;rooms&#34;).childByAutoId()
newRef.child(&#34;name&#34;).setValue(&#34;Room test&#34;)                                 
                            ref.child(&#34;devicesrooms&#34;).child(newRef.key).child(user!.uid).setValue(true)

let ev = ref.child(&#34;devicesrooms&#34;).child(newRef.key)
    var deviceNames = ()
    var deviceHandles = ()

// Listen for added devices
ev.observe(.childAdded, with: { (snapshot) -&gt; Void in
    // Retrieve device key
    let deviceKey = snapshot.key

    // Add observer on device
    var handle: UInt = 0

    handle = ref.child(&#34;devices&#34;).child(deviceKey).observe(.value, with: { (snapshot) in
      let value = snapshot.value as? NSDictionary
      let deviceName = value?[&#34;device_name&#34;] as? String ?? &#34;&#34;

      // Update or Add deviceName and handle in your dictionary
      deviceNames = deviceName
      deviceHandles = handle
    })
})

// Listen for removed devices
ev.observe(.childRemoved, with: { (snapshot) -&gt; Void in
    let deviceKey = snapshot.key

    // Remove deviceName from your dictionary
    deviceNames = nil

    // Retrieve handle from your dictionary
    if let handle = deviceHandles {
      // Remove observer on device
      ref.child(&#34;devices&#34;).child(deviceKey).removeObserverWithHandle(handle)
    }

    // Remove handle from your dictionary
    deviceHandles = nil
})
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在表格 View 中添加 Firebase 值,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/53073198/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/53073198/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在表格 View 中添加 Firebase 值