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)
})
}
}
})
어떻게해야합니까?
실행할 수 있도록
runTransactionBlock
년대 로컬 캐시에로드 문제가 셀 텍스트를 채우는 코드에서 더 많이 나타나는 것처럼 문제를 이해하는 데 충분한 정보가 부족합니다. 그러나 제품 오브젝트로 작업하는 경우 cell.likeLabel.text = productObject.ratingToShow와 같을 것입니다. – Jay제품 구조도 예입니다. –
글쎄, 당신의 질문에 코드는 개념적으로 괜찮지 만, 분명히 productName, snusNicotine 등이 빠져서 ProductObject를 채워서 코드가 작동하지 않을 것입니다. 우리가 셀을 채우는 코드를 살펴볼 필요가 있다고 생각합니다.또한 여기저기서 print 문을 던져 제품 배열의 productObject가 변경되거나 생략되지 않도록 할 수 있습니다. – Jay