gk103 gk103 - 2 months ago 11
Swift Question

Variable from viewDidLoad not populating custom table cell

I need to take variables that are populated in the viewDidLoad method to show up on labels connected to a custom cell. What i am trying to do is:


  1. Find out SKUs in user's box stored in database

  2. Use SKU to find out details of the product stored in database

  3. Store product details in appropriate variable

  4. Take said variable and populate labels in a custom table cell



The issue is that I can store the variable in the viewDidLoad method, but when I try to call the variable to populate the custom table cell, the variable is blank.

I am using Firebase to store the data. The fire base nodes are set up as the following, Node 1: Products/Sku/Item details Node 2: Box/UID/Skus

"products" : {
"0123456" : {
"brand" : "Nike",
"item_name" : "basketball"
}
},
"box" : {
"jEI5O8*****UID" : {
"sku" : "0123456"


I've been scouring through stack overflow, youtube, google, etc but i can't seem to find a solution...If you can help point me in the right direction that would be greatly appreciated! FYI I am new to swift/firebase.

import UIKit
import FirebaseAuth
import FirebaseDatabase

class drawerFaceExampleViewController: UIViewController, UITableViewDelegate,UITableViewDataSource {

var databaseRef = FIRDatabase.database().reference()
var loggedInUser = AnyObject?()
var loggedInUserData = AnyObject?()
var itemDrawer = AnyObject?()
var dataDict = AnyObject?()

@IBOutlet weak var homeTableView: UITableView!

var item_name = String()
var brand_name = String()




override internal func viewDidLoad() {
super.viewDidLoad()


self.loggedInUser = FIRAuth.auth()?.currentUser


//get the logged in users details
self.databaseRef.child("user_profiles").child(self.loggedInUser!.uid).observeSingleEventOfType(.Value) { (snapshot:FIRDataSnapshot) in

//store the logged in users details into the variable

self.loggedInUserData = snapshot

//get all the item sku's that are in the user's box

self.databaseRef.child("box/\(self.loggedInUser!.uid)").observeEventType(.ChildAdded, withBlock: { (snapshot:FIRDataSnapshot) in


let sku = snapshot.value! as! String

//access the 'products' node to extract all the item details
self.databaseRef.child("products").child(sku).observeSingleEventOfType(.Value, withBlock: { (snapshot:FIRDataSnapshot) in

if let itemvariable = snapshot.value!["item"] as? String {
self.item_name = item variable

//testing to see if item name is stored, works!
print("testing=", self.item_name)
}
if let brandvariable = snapshot.value!["brand"] as? String{
self.brand_name = brand variable

//testing to see if brand name is stored, works!
print("testingBrand =", self.brand_name)
}

})


self.homeTableView.insertRowsAtIndexPaths([NSIndexPath(forRow:0,inSection:0)], withRowAnimation: UITableViewRowAnimation.Automatic)

}){(error) in

print(error.localizedDescription)
}

}

}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}

func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

}

func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {

let cell: drawerFaceExampleTableViewCell = tableView.dequeueReusableCellWithIdentifier("drawerFaceExampleCell", forIndexPath: indexPath) as! drawerFaceExampleTableViewCell

//checking to see the item & brand name has been extracted...but blank :(

print("item_name=",self.item_name)
print("item_name=",self.item_name)

//this is where item & brand name extracted from viewDidLoad to display in the cell.
cell.configure(nil, brandName: brand_name, itemName: item_name)


return cell
}

}

Answer

Sounds like your data hasn't finished loading yet when you go to read the variable. You need to update your UI after the download is complete, in the completion handler:

if let itemvariable = snapshot.value!["item"] as? String {
    self.item_name = item variable

    //testing to see if item name is stored, works!
    print("testing=", self.item_name)
}

if let brandvariable = snapshot.value!["brand"] as? String{
    self.brand_name = brand variable

    //testing to see if brand name is stored, works!
    print("testingBrand =", self.brand_name)
}

// Update UI here.