2016-11-03 44 views
0

我使用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) 

         }) 
        } 
       } 
       }) 

我应该做什么改变?

+0

有问题中没有足够的信息来理解问题,因为在填充单元格文本的代码中问题似乎更多。但是,如果您正在使用产品对象,则会更像cell.likeLabel.text = productObject.ratingToShow – Jay

+0

我有产品结构也是。 –

+0

那么,你的问题中的代码在概念上是可以的,但是,你显然缺少productName,snusNicotine等来填充ProductObject,因此代码不能按原样工作。我认为我们需要看看填充单元的代码。此外,您可能希望在这里和那里抛出一个打印语句,以确保产品数组中的productObjects不会被更改或省略。 – Jay

回答

0

轻松地操纵在用户界面中的增量值,一般和最基本的方法是这样的: -

  • 更新您的火力地堡数据库您的帖子:使用runTransactionBlock

  • 然后在您的Product结构中检索和存储的数据中更新您当前的dataSource以获得相同的值。

对于如: - 一旦用户点击了像按钮,喜欢或不喜欢它,你更新了各自的行动在后端(火力地堡)值,以及在products[indexPath.row].ratingToShow,然后调用self.tableView.reloadData。这样,每次用户喜欢或不喜欢某个文章节省带宽时,您都不必从数据库中获取当前的喜欢数。

如果您喜欢按钮都有它@IBAction在TableViewCell类,然后只是做一个控制器参考yourTableViewController和更改数据源: -

class yourTabelViewCell : UITableViewCell{ 

var tableController : yourTableViewcontroller! 
var indexPathForCell : Int! 


    @IBAction func likeBtnAction(_ sender: UIButton) { 

    //User likes or dislikes the post.... 

     self.tableController.products[self.indexPathForCell]. ratingToShow = //Manipulate the value +1 or -1 
     self.tableController.yourTableView.reloadData() // Given yourTableView is the custom tableView you have implemented in your viewController 
    } 
    } 

为了得到indexPathForCell,只是初始化您的值cellForRowAt indexPath : _

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 

    let cell = tableView.dequeueReusableCell(withIdentifier: "yourTabelViewCell_identifier", for: indexPath) as! yourTabelViewCell 

    cell.likeLabel.text = products[indexPath.row].ratingToShow 
    cell.indexPathForCell = indexPath.row 
    return cell 
     } 

而且不必担心用户失去网络连接,同时喜欢或不喜欢的岗位,runTransactionBlock的在本地缓存加载所以一旦你的用户恢复网络连接,这些命令将被执行

+0

问题是,它不喜欢,它是评级。我得计算一下。所以我不能在美观上增加价值。喜欢的系统很容易。 –

+0

通过评级你平均?只需将算法放在一起,一旦用户对ratersCount进行评级,就可以将这些算法增加到+1,然后对用户评分(在数据源和后端同时评分)进行评分,并计算出来并在UI中展示,只需制作一个调用你的后端,这些变化瞬间反映出来,它唯一的数学,喜欢和评分都可能是相同的,或者只是很少的数学操作。 – Dravidian

+0

我有这个算法。如果你看看我的代码,那么你应该看到它。 –