我需要获取viewDidLoad方法中填充的变量,以显示连接到自定义单元格的标签。我想做的是:
问题是我可以将变量存储在viewDidLoad方法中,但是当我尝试调用变量来填充自定义表格单元格时,该变量是空白的。
我正在使用Firebase来存储数据。火基节点设置如下,节点1:产品/ Sku /项目详细信息节点2:Box / UID / Skus
"products" : {
"0123456" : {
"brand" : "Nike",
"item_name" : "basketball"
}
},
"box" : {
"jEI5O8*****UID" : {
"sku" : "0123456"
我一直在搜索堆栈溢出,youtube,谷歌等,但我似乎无法找到解决方案...如果你能帮我指出正确的方向,我将不胜感激!仅供参考我是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
}
}
答案 0 :(得分:0)
当您去阅读变量时,听起来您的数据还没有完成加载。下载完成后,您需要在完成处理程序中更新UI:
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.