Retrieve All Child Value to a Label in Table View Cell

Retrieve all child value to a label in table view cell

So based on comments, as you said that you only need cakename and its rate to fill in the table. Other then that is static (if not than please go for a structure and answer suggested by @dstepan) .. So here is how you can fill up your data in tableview.

   var cakes = [Cake]()

override func viewDidLoad() {
super.viewDidLoad()

let almondSnap = ref.child("Hyderabad").child("Bakery").child("Cake")
almondSnap.observe( .value, with: { (snapshot) in
if snapshot.hasChildren(){
for snap in snapshot.children {
// snap.key produces name of cake
// snap.value produces rate of cake

if let node = snap as? FIRDataSnapshot, let rate = node.value as? Int {
self.cakes.append(Cake(cakeEngNames: node.key,
cakeUrNames: "badam cake",
cakeImages: UIImage(named:"Maru")!,
cakeRates: String(rate)))
}
}
self.cakeTableView.reloadData()
}
})
}

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

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
}

Retrieve child key one by one and display in label swift

You'll of course need to create a tableViewController and link it up to a TableView and assign your cell its own identifier, but this is a good outline of what you'd need to do in your own tableViewController:

class tableViewController: UITableViewController {

var dataArray = [String]()
var ref = Database.database().reference() //You didn't include your delcaration of ref in your code, so don't use this, just use the one you were already using.

override func viewDidLoad() {
super.viewDidLoad()
retrieve()
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

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

override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell")
let data = dataArray[indexPath.row]
cell.textLabel?.text = data

return cell
}

func retrieve(){

let userId = Auth.auth().currentUser?.uid

let intakeRef = Database.database().reference().child("Users").child(userId!).child("Intake")

intakeRef.observeSingleEvent(of: .value, with: { (snapshot) in

if let intake = snapshot.value{

self.ref.child("Attendance").child("Checkin").child(intake as! String).child(userId!).child("BM050-3-3-IMNPD").observe(.value, with: { (snapshot) in

for child in snapshot.children{
let key = (child as AnyObject).key as String
self.dataArray.append(key)

}

})
self.tableView.reloadData()
}
}) { (Error) in
print("unable to retrieve name from nameRef")
}

}

}

Again you'll need to create your own UItableView, link it up to a UITableViewController, assign your own cell reuse identifier and make sure that cellForRowAt has the correct reuse identifier in it, and you'll need to change the ref variable in my provided code to the ref that you need to use.

Access Parent tableViewCell from Child CollectionViewCell

please try this

  guard  let lbl = (self.yourTableView.cellForRow(at: [NSIndexPath(row: 0, section: 0) as IndexPath]) as! yourCellNameInTableView).labelName else {
return
}
lbl.text = "Your text"

Please pass your row number and section number. It may helps to you.Thanks you

Custom uitableview cell not showing all text labels

You are using the standard UITableViewCell and you assign all three values to the same label.

You have to cast the cell to the custom cell and assign the values to the custom labels

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

// let use a hack for now, we actually need to dequeue our cells for memory efficiency
// let cell = UITableViewCell(style: .Subtitle, reuseIdentifier: cellId)

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

let user = users[indexPath.row]
cell.nameLabel?.text = user.date_clasname
cell.teacherLabel?.text = user.teacher
cell.roomLabel?.text = user.room_number

return cell
}

Replace nameLabel, teacherLabel and roomLabel with the real property names.

And please conform to the naming convention and name variables lowerCamelCased for example dateClasname and roomNumber

Pass Data (Label) from TableViewCell to another ViewController

Quick solution:

  1. Change
    performSegue(withIdentifier: "yourSegueIdentifer", sender: self) to performSegue(withIdentifier: "yourSegueIdentifer", sender: valueToPass)

2.Your prepare for Segue method should looks like this:

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "yourSegueIdentifer" {
let destinationViewController = segue.destination as! AnotherViewController
destinationViewController.valueToPass = sender as? String
}
}

  1. On AnotherViewController create var valuteToPass: String = "" and set your label.text = valueToPass

But I think you should not use currentCell.textLabel.text value, instead use the original value. (like if you set your currentCell as cell.textLabel.cell = array[indexPath.row], your valueToPass should be valueToPass = array[indexPath.row])

EDIT:

You use didDeselectRowAt method, instead of didSelectRowAt.

Change func tableView(_ tableView: UITableView, didDeselectRowAt indexPath: IndexPath) to
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath)

Don't use global variable, create it in didSelectRowAt.

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("You selected cell #\(indexPath.row)!")

// Get Cell Label
let indexPath = tableView.indexPathForSelectedRow!
let currentCell = tableView.cellForRow(at: indexPath)! as UITableViewCell

let valueToPass = currentCell.textLabel?.text
print("value: \(valuteToPass)")

performSegue(withIdentifier: "toDetails", sender: valueToPass)
}

On DestinationController:

class DestinationController: UIViewController {

var valuteToPass: String = ""

override func viewDidLoad() {
super.viewDidLoad()
jobLabel.text = valueToPass
}

}

EDIT2

JobTableViewController

  1. delete var valueToPass:String!

Change let valueToPass = jobs[indexPath.row].text instead of let valueToPass = currentCell.textLabel?.text
I checked this change in your code, this will work.

How to pass the value of a label in my TableCell to the TableViewController on the click of a button in the TableCell?

You can pass the label value in the closure itself along with sender.

class PledgeTableViewCell: UITableViewCell {

var plusBtnAction: ((String) -> Void)?
var minusBtnAction: ((String) -> Void)?

@IBAction func minusBtn(_ sender: Any) {

if var tickCount = Int(ticketCountLabel.text!) {

if(tickCount > 0)
{
tickCount -= 1
ticketCountLabel.text = String(tickCount)

}
self.minusBtnAction?(tickCount)
}
}

@IBAction func plusBtn(_ sender: AnyObject) {

if var tickCount = Int(ticketCountLabel.text!) {

//I WANT TO SEND THIS 'TICKCOUNT' TO THE PLEDGEVIEWCONTROLLER
tickCount += 1
ticketCountLabel.text = String(tickCount)
self.plusBtnAction?(ticketCount)
}

}
}

in cellForRow: method in PledgeViewController

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

let cell = tableView.dequeueReusableCell(withIdentifier: "TakePledgeCell", for: indexPath) as! PledgeTableViewCell
.....
cell.plusBtnAction = { labelText in
//handle labelText here
}
}


Related Topics



Leave a reply



Submit