Xcodian Solangi
Xcodian Solangi

Reputation: 2408

Retrieve child value from firebase to an array to use in a table view cell

I am retrieving almondRate from Snapshot to an array cakeRatesin this class. but there is error -- use of unresolved identifier almondRate. How can i get the value of almondRate from snapshot to put in an array.
I think this may the problem of scope of a variable, help me to overcome this please!

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



Update: Retrieve all child values of "cake" to 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
}
}

Upvotes: 0

Views: 322

Answers (1)

Nirav D
Nirav D

Reputation: 72440

You cannot access the block variable outside the block, also instead of adding multiple array you need to use one array of custom type object.

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

Now create the array struct Cake object like [Cake] access that array.

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")]

Now when you get response access the first object from array and set the rate of it.

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
}

Upvotes: 1

Related Questions