有人知道为什么这不起作用吗? tableView是空的,即使数据库和存储中有项目也没有显示任何内容。这在我实现从存储中加载图像之前工作正常,您将在我已粘贴的代码的底部看到。food.append()
语句曾经在storageRef.getData()
闭包之外(因为它并不存在)但是,如果我现在把它拿出来,它就无法访问recipeImage
,因为它在闭包内声明了。它不起作用,因为它关闭了吗?如果是这样,我该如何解决?
let parentRef = Database.database().reference().child("Recipes")
let storage = Storage.storage()
parentRef.observe(.value, with: { snapshot in
//Processes values received from server
if ( snapshot.value is NSNull ) {
// DATA WAS NOT FOUND
print("– – – Data was not found – – –")
} else {
//Clears array so that it does not load duplicates
food = []
// DATA WAS FOUND
for user_child in (snapshot.children) {
let user_snap = user_child as! DataSnapshot
let dict = user_snap.value as! [String: String?]
//Defines variables for labels
let recipeName = dict["Name"] as? String
let recipeDescription = dict["Description"] as? String
let downloadURL = dict["Image"] as? String
let storageRef = storage.reference(forURL: downloadURL!)
storageRef.getData(maxSize: 1 * 1024 * 1024) { (data, error) -> Void in
let recipeImage = UIImage(data: data!)
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))
}
}
self.tableView.reloadData()
}
})
答案 0 :(得分:1)
移动
self.tableView.reloadData()
之后
food.append(Element(name: recipeName!, description: recipeDescription!, image: recipeImage!))