gpt4 book ai didi

ios - 如何读取firebase数据库并将其放入tableview单元格问题

转载 作者:行者123 更新时间:2023-11-30 12:24:42 26 4
gpt4 key购买 nike

我是一个快速的初学者,我想从 firebase 数据库中获取值,但它总是收到两次相同的字典结构,并且当我解开它崩溃时无法将值放入 TableView 单元格中......

这是我的 JSON 格式

enter image description here

代码工作

import UIKit
import Firebase
//import FirebaseAuthUI
//import FirebaseGoogleAuthUI
//import FirebaseFacebookAuthUI

let device = FIRDatabase.database().reference()

class MainTableViewController: UITableViewController
{
var dic:NSDictionary?

override func viewDidLoad()
{
super.viewDidLoad()

//獲取當前登陸用戶
FIRAuth.auth()?.addStateDidChangeListener(self.UserAlive(auth:user:))
print("主畫面viewDidLoad")
}

func UserAlive(auth: FIRAuth, user: FIRUser?)
{
if user == nil
{

self.present((self.storyboard?.instantiateViewController(withIdentifier: "SignIn"))!, animated: true, completion: nil)
}
else
{
csGolbal.g_User = user
CheckData()
}
}

func CheckData()
{
print("CHECKDATA")
let ref = device.child("USER").child(csGolbal.g_User!.email!.replacingOccurrences(of: ".", with: "_"))

ref.observeSingleEvent(of: .value, with:
{ (snapshot) in
if snapshot.exists()
{
csGolbal.g_key = ((snapshot.value as AnyObject).allKeys)!
}
ref.child(csGolbal.g_key![0] as! String).observeSingleEvent(of: .value, with:
{ (snapshot) in
// Get user value
self.dic = snapshot.value as? NSDictionary
print(self.dic)
//self.tableView.reloadData()
})
})

}

这是我不明白如何输入

  override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// #warning Incomplete implementation, return the number of rows

if let number = csGolbal.g_key?.count
{
return number
}
else
{
return 0
}
}

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
{
let cell = tableView.dequeueReusableCell(withIdentifier: "MainTableViewCell", for: indexPath) as! MainTableViewCell

//put in here
// label.text and ImageView

return cell
}

请帮助我,告诉我哪里做错了。

@dahiya_boy 我尝试你的功能

func getDataFromDB()
{
DispatchQueue.main.async( execute: {

//let dbstrPath : String! = "Firebase Db path"
let ref = device.child("USER").child(csGolbal.g_User!.email!.replacingOccurrences(of: ".", with: "_"))

ref.observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists()
{
print("snapshot not exists")
}
else
{

for item in snapshot.children
{
let number = item as! FIRDataSnapshot
var aDictLocal : [String : String] = number.value! as! [String : String]
aDictLocal.updateValue(number.key, forKey: "key")

print("value \(number.value!) And Key \(number.key)") // Here you got data
}
}
self.tableView.reloadData()
})
})

}

两次结果反馈

enter image description here

最佳答案

实际上,您已将数据以随 secret 钥存储在数据库中,因此请使用下面的函数

func getDataFromDB(){
DispatchQueue.main.async( execute: {

let dbstrPath : String! = "Firebase Db path)"

dbRef.child(dbstrPath!).observeSingleEvent(of: .value, with: { (snapshot) in
if !snapshot.exists(){

print("snapshot not exists")

}
else{
self.arrEmail.removeAll() // Add this
for item in snapshot.children {
let number = item as! FIRDataSnapshot
var aDictLocal : [String : String] = number.value! as! [String : String]
aDictLocal.updateValue(number.key, forKey: "key")
self.arrEmail.append(aDictLocal) // add this
print("value \(number.value!) And Key \(number.key)") // Here you got data
}
}
// self.tblContacts.reloadData()
})
})

}

Edit

在您的 VC 中创建一个如下所示的全局数组

    var arrEmail = [[String : String]]() // Assuming your key and value all string

在上面的代码中添加两行(我编辑并带有注释添加此)

self.arrEmail.removeAll()

self.arrEmail.append(aDictLocal) // Now in arrEmail you have all the values for every random key.

关于ios - 如何读取firebase数据库并将其放入tableview单元格问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44341178/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com