我使用Firebase值创建了一些简单的计算系统。但是,如果我评价它,计算和值是正确的,但它不会更新标签。标签上的Firebase值未更新
这就是我得到的值,做计算:
let ref = FIRDatabase.database().reference().child("Snuses").queryOrdered(byChild: "Brand")
.queryEqual(toValue: brandName)
ref.observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.exists(){
let enumerator = snapshot.children
while let thisProduct = enumerator.nextObject() as? FIRDataSnapshot {
// Chances are you'd have to create a dictionary
let thisProductDict = thisProduct.value as! [String:AnyObject]
let rating = thisProductDict["rating"] as! Double
let ratersCount = thisProductDict["ratersCount"] as! Double
let ratingToShow: String = String((ratersCount == 0) ? 0 : rating/ratersCount)
let productObject = Product(
rating: rating,
ratersCount: ratersCount,
ratingToShow: ratingToShow)
self.products.append(productObject)
}
self.tableView.reloadData()
而在cellForRowAtIndexPath
我试图说明标签上的ratingToShow:
cell.ratingLabel.text = products[indexPath.row].ratingToShow
我添加值是这样的方式:
let ratingToShow: String = String((products[indexPath.row].ratersCount == 0) ? 0 : products[indexPath.row].rating/products[indexPath.row].ratersCount)
cell.likeLabel.text = ratingToShow
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).child(self.currentUser.generalDetails.uid).observeSingleEvent(of: .value, with: { (snapshot) in
if snapshot.value as? Bool == true{
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).child("rating").observeSingleEvent(of: .value, with: { (snapshot) in
let currentUserRate = snapshot.value
cell.ratingView.rating = currentUserRate as! Double
})
}else{
cell.ratingView.rating = 0.0
}
cell.ratingView.didFinishTouchingCosmos = { rating in
if snapshot.value as? Bool == true{
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).child("rating").observeSingleEvent(of: .value, with: { (snapshot) in
let currentUserRate = snapshot.value as? Double
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("rating").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! - currentUserRate!
cell.update(rating)
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("rating").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! + rating
cell.update(rating)
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Bool
if (value == nil) {
value = true
}
currentData.value = [self.currentUser.generalDetails.uid:true]
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).updateChildValues(["rating": rating])
return FIRTransactionResult.success(withValue: currentData)
})
})
}else{
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("ratersCount").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! + 1
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).child("rating").runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Double
if (value == nil) {
value = 0.0
}
currentData.value = value! + rating
cell.update(rating)
return FIRTransactionResult.success(withValue: currentData)
})
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({
(currentData:FIRMutableData!) in
var value = currentData.value as? Bool
if (value == nil) {
value = true
}
currentData.value = [self.currentUser.generalDetails.uid:true]
self.databaseRef.child("productRatings").child(self.currentUser.generalDetails.uid).child(self.products[indexPath.row].snusProductTitle).updateChildValues(["rating": rating])
return FIRTransactionResult.success(withValue: currentData)
})
}
}
})
我应该做什么改变?
有问题中没有足够的信息来理解问题,因为在填充单元格文本的代码中问题似乎更多。但是,如果您正在使用产品对象,则会更像cell.likeLabel.text = productObject.ratingToShow – Jay
我有产品结构也是。 –
那么,你的问题中的代码在概念上是可以的,但是,你显然缺少productName,snusNicotine等来填充ProductObject,因此代码不能按原样工作。我认为我们需要看看填充单元的代码。此外,您可能希望在这里和那里抛出一个打印语句,以确保产品数组中的productObjects不会被更改或省略。 – Jay