从firebase检索子值到要在表视图单元

时间:2017-03-04 11:11:35

标签: ios arrays swift firebase scope

我正在从此快照中将 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
}
}

1 个答案:

答案 0 :(得分:1)

您无法访问块外的块变量,也无需添加多个数组,您需要使用一个自定义类型对象数组。

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

现在创建数组struct 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
}