gpt4 book ai didi

ios - 从 firebase 检索子值到数组以在 TableView 单元格中使用

转载 作者:行者123 更新时间:2023-11-28 15:48:57 25 4
gpt4 key购买 nike

我正在将 almondRate 从 Snapshot 检索到此类中的数组 cakeRates。但存在错误——使用了未解析的标识符 almondRate。我如何从快照中获取 almondRate 的值以放入数组中。
我认为这可能是变量范围的问题,请帮助我解决这个问题!

import UIKit
import Firebase
import FirebaseStorage

class TestTableViewController: UITableViewController {

@IBOutlet var cakeTableView: UITableView!

var ref = FIRDatabase.database().reference()

var cakeEngNames = ["almond Cake", "Jelly Cake"]
var cakeUrNames = ["badam cake", "jeli ka cake"]
var cakeImages = [UIImage(named: "Maru")!, UIImage(named: "spices")!]
var cakeRates = ["\(almondRate)", "45"]

override func viewDidLoad() {
super.viewDidLoad()

let AlmondSnap = ref.child("Karachi").child("Bakery").child("Cake").child("Almond_Cake")
AlmondSnap.observeSingleEvent(
of: .value, with: { (snapshot) in
let almondRate = snapshot.value as! String
self.cakeRates.append(almondRate)
print("\(almondRate)")
})
}

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

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

cell.cakeImage.image = cakeImages[indexPath.row]
cell.cakeEngLabs.text = cakeEngNames[indexPath.row]
cell.cakeUrLabs.text = cakeUrNames[indexPath.row]
cell.cakeRateLabs.text = cakeRates[indexPath.row]
return cell
}
}

enter image description here



更新:将“cake”的所有子值检索到 cakeRateLabs

import UIKit
import Firebase
import FirebaseStorage

class TestTableViewController: UITableViewController {

@IBOutlet var cakeTableView: UITableView!

var ref = FIRDatabase.database().reference()


struct Cake {
let cakeEngNames: String
let cakeUrNames: String
let cakeImages: UIImage
var cakeRates: String
}


var cakes = [Cake(cakeEngNames: "almond Cake", cakeUrNames: "badam cake", cakeImages: UIImage(named:"Maru")!, cakeRates: ""),

Cake(cakeEngNames: "Jelly Cake", cakeUrNames: "jeli ka cake", cakeImages: UIImage(named:"spices")!, cakeRates: ""),

Cake(cakeEngNames: "Jelly Cake", cakeUrNames: "jeli ka cake", cakeImages: UIImage(named:"spices")!, cakeRates: ""),

Cake(cakeEngNames: "Jelly Cake", cakeUrNames: "jeli ka cake", cakeImages: UIImage(named:"spices")!, cakeRates: "")]




override func viewDidLoad() {
super.viewDidLoad()

let AlmondSnap = ref.child("Karachi").child("Bakery").child("Cake").child("Almond_Cake")
AlmondSnap.observe( .value, with: { (snapshot) in
let almondRate = snapshot.value as! String
self.cakes[0].cakeRates = almondRate

print("\(almondRate)")
self.tableView.reloadData()
})
}

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

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

cell.cakeImage.image = self.cakes[indexPath.row].cakeImages
cell.cakeEngLabs.text = self.cakes[indexPath.row].cakeEngNames
cell.cakeUrLabs.text = self.cakes[indexPath.row].cakeUrNames
cell.cakeRateLabs.text = self.cakes[indexPath.row].cakeRates
return cell
}
}

最佳答案

您不能在 block 外访问 block 变量,而且您需要使用一个自定义类型对象数组而不是添加多个数组。

struct Cake {
var cakeEngName: String
var cakeUrName: String
var cakeImage: UIImage
var cakeRate: String
}

现在创建数组结构 Cake 对象,如 [Cake] 访问该数组。

var cakes = [Cake(cakeEngNames: "almond Cake", cakeUrName: "badam cake", cakeImage: UIImage(named:"Maru")!, cakeRate: ""),
Cake(cakeEngNames: "Jelly Cake", cakeUrName: "jeli ka cake", cakeImage: UIImage(named:"spices")!, cakeRate: "45")]

现在,当您获得响应时,访问数组中的第一个对象并设置它的速率。

override func viewDidLoad() {
super.viewDidLoad()

let AlmondSnap = ref.child("Karachi").child("Bakery").child("Cake").child("Almond_Cake")
AlmondSnap.observeSingleEvent(
of: .value, with: { (snapshot) in
let almondRate = snapshot.value as! String
self.cakes[0].cakeRate = almondRate
print("\(almondRate)")
self.tableView.reloadData()
})
}

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

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

cell.cakeImage.image = self.cakes[indexPath.row].cakeImage
cell.cakeEngLabs.text = self.cakes[indexPath.row].cakeEngName
cell.cakeUrLabs.text = self.cakes[indexPath.row].cakeUrName
cell.cakeRateLabs.text = self.cake[indexPath.row].cakeRate
return cell
}

关于ios - 从 firebase 检索子值到数组以在 TableView 单元格中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42595282/

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