我正在尝试在xcode上对Firebase数据库进行选择并将结果显示在表格上,但我得到的结果非常奇怪。就像它在同一条目上运行for循环一样。很可能是因为桌子。
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "ItemCell", for: indexPath)
let groceryItem = items[indexPath.row]
// Below is code to query users in Firebase to check if it is the current user.
let selection = ref.queryOrdered(byChild: "addedByUser").queryEqual(toValue : user.email)
selection.observeSingleEvent(of: .value, with:{ (snapshot: FIRDataSnapshot) in
if let value = snapshot.value as? [String: Any] {
print(value)
}
})
//End of code
//if user.email == groceryItem.addedByUser {
cell.textLabel?.text = groceryItem.name
cell.detailTextLabel?.text = groceryItem.addedByUser
// }
toggleCellCheckbox(cell, isCompleted: groceryItem.completed)
return cell
}
和Firebase是:
{
"expense-items" : {
"20170509" : {
"addedByUser" : "michael_papado@crymary.gr",
"amount" : 37,
"categoryType" : "Shopping",
"completed" : false,
"name" : "Dijon",
"paymentType" : "Cash"
},
"zambon" : {
"addedByUser" : "harry@crymary.gr",
"amount" : 13,
"categoryType" : "Shopping",
"completed" : false,
"name" : "zambon",
"paymentType" : "CreditCard"
},
"zouzouni" : {
"addedByUser" : "harry@crymary.gr",
"amount" : 24,
"categoryType" : "Kids",
"completed" : false,
"name" : "zouzouni",
"paymentType" : "Cash"
}
}
}
只有20170509来自登录的用户,我获得了20170509条目的3倍。 我如何计算选择的结果,以便用正确的结果填充表格?
答案 0 :(得分:0)
所以这一切都是错误的。我不应该检查tableView函数上的列表。 我所要做的就是在viewDidLoad中填充一个新列表并一直使用它。
if FIRAuth.auth()?.currentUser != nil {
let selection = ref.queryOrdered(byChild: "addedByUser").queryEqual(toValue : FIRAuth.auth()?.currentUser?.email)
selection.observeSingleEvent(of: .value, with:{ (snapshot: FIRDataSnapshot) in
var newItems: [ListItem] = []
for item in snapshot.children {
let listItem = ListItem(snapshot: item as! FIRDataSnapshot)
newItems.append(listItem)
}
self.items = newItems
self.tableView.reloadData()
})
}