All the tableview functions are working except cell for row index path .
除了用于行索引路径的单元格外,所有tableview函数都在工作。
The problem maybe that foods array is empty so the number for rows is 0 so the cell for row at index path is not called
问题可能是food数组为空,因此行数为0,因此不会调用索引路径中行的单元格
@IBOutlet weak var foooods: UITableView!
var databaseref = Database.database().reference()
var img : AnyObject?
var foods = [String?]()
override func viewDidLoad() {
super.viewDidLoad()
self.databaseref.child("basic food").observe(.childAdded, with: {( snap: DataSnapshot) in
let snapp = snap.value as! [String:AnyObject]
if let x = snapp["name"] as! String? {
self.foods.insert(x, at: 0)
//self.foods.append(x)
}
})
self.foooods.reloadData()
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.foods.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
print("difufuehf")
let cell : foodsTableViewCell = tableView.dequeueReusableCell(withIdentifier: "aupa", for:indexPath) as! foodsTableViewCell
print("fufvksdfvysdgfvjdsgfdsygfvds,jhvjsdvsdjvguydsfgdsylfgdsyfgsdlygfsiygf")
if let foo = foods[indexPath.row] {
print(foo)
cell.food.text = foo
}
return cell
}
1 个解决方案
#1
1
This must be a duplicate but I can't find one.
这必须是重复但我找不到。
Your issue is that you call reloadData
in the wrong place which results in it being called far too soon. You need to call it inside the completion block, after you update your data model.
你的问题是你在错误的地方调用reloadData导致它被太快地调用。更新数据模型后,需要在完成块内调用它。
And you need to make sure it gets called on the main queue.
而且你需要确保它在主队列中被调用。
override func viewDidLoad() {
super.viewDidLoad()
self.databaseref.child("basic food").observe(.childAdded, with: {( snap: DataSnapshot) in
if let snapp = snap.value as? [String:Any], let x = snapp["name"] as? String {
self.foods.insert(x, at: 0)
//self.foods.append(x)
DispatchQueue.main.async {
self.foooods.reloadData()
}
}
})
}
Note that I also fixed the way the value is obtained. You really need to avoid force unwrapping and force casting.
请注意,我还修复了获取值的方式。你真的需要避免强行展开和强制施放。
#1
1
This must be a duplicate but I can't find one.
这必须是重复但我找不到。
Your issue is that you call reloadData
in the wrong place which results in it being called far too soon. You need to call it inside the completion block, after you update your data model.
你的问题是你在错误的地方调用reloadData导致它被太快地调用。更新数据模型后,需要在完成块内调用它。
And you need to make sure it gets called on the main queue.
而且你需要确保它在主队列中被调用。
override func viewDidLoad() {
super.viewDidLoad()
self.databaseref.child("basic food").observe(.childAdded, with: {( snap: DataSnapshot) in
if let snapp = snap.value as? [String:Any], let x = snapp["name"] as? String {
self.foods.insert(x, at: 0)
//self.foods.append(x)
DispatchQueue.main.async {
self.foooods.reloadData()
}
}
})
}
Note that I also fixed the way the value is obtained. You really need to avoid force unwrapping and force casting.
请注意,我还修复了获取值的方式。你真的需要避免强行展开和强制施放。