【发布时间】:2017-01-31 05:57:20
【问题描述】:
在运行打印语句后,我的 cellForRowAtIndexPath 没有被调用。发现我的 tableView 没有重新加载...尝试了几个地方,但仍然没有重新加载。有人知道为什么吗?`
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
DataService.ds.DATABASE_REF_USERS.child(CURRENTUSER!).child("cart").observe(.value, with: { (snapshot) in
print("check")
if let snapshot = snapshot.children.allObjects as? [FIRDataSnapshot] {
self.cartItems = []
for snap in snapshot {
let shoeId = snap.key
DataService.ds.DATABASE_REF_SHOES.child("upcomingShoes").child(shoeId).observeSingleEvent(of: .value, with: { (snapshot) in
if let shoeData = snapshot.value as? Dictionary<String, AnyObject> {
let shoe = Shoe(shoeId: shoeId, shoeData: shoeData)
print(shoe.shoeName)
self.cartItems.append(shoe)
}
})
}
}
self.tableView.reloadData()
})
print(self.cartItems.count)
}
//添加到我的数据库:
@IBAction func addToCartPressed(_ sender: Any) {
//Under the current users, child node called "cart", I want to add the current shoes id
let currentUserCartRef = DataService.ds.DATABASE_REF_USERS.child(CURRENTUSER!).child("cart")
let shoeDataToAdd = [shoe.shoeId: true]
currentUserCartRef.updateChildValues(shoeDataToAdd)
}
`
表格视图单元格方法
//Table View Methods
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return cartItems.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let shoe = cartItems[indexPath.row]
if let cell = tableView.dequeueReusableCell(withIdentifier: "cartCell", for: indexPath) as? CartCell {
if let shoeImage = MainVC.imageCache.object(forKey: shoe.shoeImageUrl as NSString) {
cell.configureCell(shoe: shoe, shoeImage: shoeImage)
}
else {
cell.configureCell(shoe:shoe)
}
return cell
}
else {
return PreviewCell()
}
}
【问题讨论】:
-
我的班级名列前茅:var cartItems = [Shoe]()
-
该打印语句在执行
self.tableView.reloadData()行之前执行。所以很明显它打印0作为输出。您还需要在主线程上重新加载 tableView。 -
嘿,我只是在尝试不同的区域,看看代码是否正在运行,打印语句是否在那里运行,或者 self.tableView.reloadData() 是否在哪里运行
-
@NiravD 如何在主线程上调用 reload?
-
应该是
DispatchQueue.main.async { self.tableView.reloadData() }
标签: ios swift uitableview firebase firebase-realtime-database