【问题标题】:Get parent key in UITableview with firebase使用firebase在UITableview中获取父键
【发布时间】:2018-01-11 18:12:53
【问题描述】:

我的 firebase 数据库结构如下:

Snap (-KWLSAIh5WJvNJOkxBEr) {
  beschrijving = "description";
  image = "link to image";
  title = "title";
}
Snap (-KWLSTak0H20X_2Qnanv) {
  beschrijving = "description";
  image = "link to image";
  title = "title";
}

这是我用来在 TableView 中显示它的代码:

import UIKit
import Firebase

class NieuwsTableViewController: UITableViewController {

var users = [UsersII]()
let cellId = "IdCell"

override func viewDidLoad() {
    super.viewDidLoad()

    fetchUser()
}
func fetchUser() {
    Database.database().reference().child("Blog").observe(.childAdded, with: { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject] {
            let user = UsersII(dictionary: dictionary)
            self.users.append(user)

            print(snapshot)


            DispatchQueue.main.async(execute: {
                self.tableView.reloadData()
            })

        }


    }, withCancel: nil)
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> lllTableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)

    let user = users.reversed()[indexPath.row]
    cell.textLabel?.text = user.name

    return cell as! lllTableViewCell
}

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    let message = users.reversed()[indexPath.row]

    guard let beschrijving = message.beschrijving else {
        return
    }
    guard let image = message.plaatje else {
        return
    }
    guard let titel = message.name else {
        return
    }


    UserDefaults.standard.set(beschrijving, forKey: "nieuwsBeschrijving")
    UserDefaults.standard.set(image,forKey: "nieuwsPlaatje")
    UserDefaults.standard.set(titel, forKey: "nieuwsTitel")


    self.performSegue(withIdentifier: "gotonews", sender: nil)
}

}

我不知道您是否需要这个来回答这个问题,但我也会发布“UsersII”(定义为 viewDidLoad 方法上方的用户),以防需要回答这个问题。

 import UIKit

class UsersII: NSObject {
 var name: String?
 var beschrijving: String?
 var plaatje: String?
 init(dictionary: [String: Any]) {
    self.name = dictionary["title"] as? String ?? ""
    self.beschrijving = dictionary["beschrijving"] as? String ?? ""
    self.plaatje = dictionary["image"] as? String ?? ""
 }
}

所以我想要实现的是,如果您单击其中一个单元格,您将获得文章的父 ID,因此在这种情况下,这将是我上面在我的 firebase 中提到的“-KWLSAIh5WJvNJOkxBEr 或 -KWLSTak0H20X_2Qnanv”数据库结构。

【问题讨论】:

  • 您可以通过调用 firebse 中的 snapshot.key 来获取文章的父 ID 子添加的观察者,然后将其添加到您的模型类中。
  • 另外,您应该将AnyObject 更改为Any,因为这是 Swift 方式。为什么UsersII 需要是NSObject 的子类?
  • @Y_Y 我的模型类是什么?如果这是一个愚蠢的问题,我很抱歉,我对这一切有点陌生。

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


【解决方案1】:

这是我要你做的: 您的模型类:

class UsersII: NSObject {
var parentId: String?
var name: String?
var beschrijving: String?
var plaatje: String?
init(dictionary: [String: Any],parentId:String) {
 self.name = dictionary["title"] as? String ?? ""
 self.beschrijving = dictionary["beschrijving"] as? String ?? ""
 self.plaatje = dictionary["image"] as? String ?? ""
 self.parentId = parentId
 }
}

获取用户方法:

  func fetchUser() {
   Database.database().reference().child("Blog").observe(.childAdded,        with: { (snapshot) in

    if let dictionary = snapshot.value as? [String: AnyObject] {
        let user = UsersII(dictionary: dictionary,parentId:snapshot.key)
        self.users.append(user)

        print(snapshot)


        DispatchQueue.main.async(execute: {
            self.tableView.reloadData()
        })

    }


 }, withCancel: nil)
}

最后你做了选择:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
let message = users.reversed()[indexPath.row]

guard let beschrijving = message.beschrijving else {
    return
}
guard let image = message.plaatje else {
    return
}
guard let titel = message.name else {
    return
}

guard let parentId = message.name else
{
   return
 }

UserDefaults.standard.set(beschrijving, forKey: "nieuwsBeschrijving")
UserDefaults.standard.set(image,forKey: "nieuwsPlaatje")
UserDefaults.standard.set(titel, forKey: "nieuwsTitel")
UserDefaults.standard.set(parentId,forKey: "nieuwsParentId")

self.performSegue(withIdentifier: "gotonews", sender: nil)
 }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-09
    • 2020-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-11-09
    • 1970-01-01
    • 2019-12-20
    • 1970-01-01
    相关资源
    最近更新 更多