gpt4 book ai didi

ios - viewDidLoad 中的变量未填充自定义表格单元格

转载 作者:行者123 更新时间:2023-11-30 13:05:27 27 4
gpt4 key购买 nike

我需要采用 viewDidLoad 方法中填充的变量来显示在连接到自定义单元格的标签上。我想做的是:

  1. 查找数据库中存储的用户盒子中的 SKU
  2. 使用SKU查找数据库中存储的产品详细信息
  3. 将产品详细信息存储在适当的变量中
  4. 获取所述变量并在自定义表格单元格中填充标签

问题是我可以将变量存储在 viewDidLoad 方法中,但是当我尝试调用该变量来填充自定义表格单元格时,该变量为空。

我正在使用 Firebase 来存储数据。 Fire Base节点设置如下,节点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
}

}

最佳答案

听起来当您去读取变量时您的数据尚未完成加载。下载完成后,您需要在完成处理程序中更新 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.

关于ios - viewDidLoad 中的变量未填充自定义表格单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39432013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com