2016-10-09 8 views
1

テーブルビューのセルに似たシステムを作成しました。しかしそれには問題があります。テーブルビューセル内のシステムのようなFirebase

  1. 私は1つのことが好きなら、7番目のセルはすべて同じようになっています。なぜですか?再利用可能なセルがありますか?
  2. これを実行する最善の方法は何ですか、私はそれを完全に間違っていますか?

これは、のようなボタンシステムである:

cell.likeButton.addTarget(self, action: #selector(self.tapped), for: .touchUpInside) 
    func tapped(sender: DOFavoriteButton) { 
      if sender.isSelected { 
       // deselect 
       sender.deselect()//+1 like 
      } else { 
       // select with animation 
       sender.select()//-1 like 
      } 
     } 

そして、これは私のlikeSystem機能である:

func likeSystem(sender: DOFavoriteButton, cellForRowAt indexPath: IndexPath){ 
     if sender.isSelected { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "snusProductsCell", for: indexPath) as! SnusProductTableViewCell 
     self.databaseRef.child("Snuses").child(self.products[indexPath.row].snusProductTitle).runTransactionBlock({ 
      (currentData:FIRMutableData!) -> FIRTransactionResult in 
      if var post = currentData.value as? [String : AnyObject], let uid = FIRAuth.auth()?.currentUser?.uid { 
       var stars : Dictionary<String, Bool> 
       stars = post["hasLiked"] as? [String : Bool] ?? [:] 
       var starCount = post["likes"] as? Int ?? 0 
       if let _ = stars[uid] { 
        // Unstar the post and remove self from stars 
        starCount -= 1 
        stars.removeValue(forKey: uid) 

       } else { 
        // Star the post and add self to stars 
        starCount += 1 

        stars[uid] = true 
        sender.deselect() 
       } 
       post["hasLiked"] = starCount as AnyObject? 
       post["likes"] = stars as AnyObject? 

       // Set value and report transaction success 
       currentData.value = post 

       return FIRTransactionResult.success(withValue: currentData) 
      } 
      return FIRTransactionResult.success(withValue: currentData) 
     }) { (error, committed, snapshot) in 
      if let error = error { 
       print(error.localizedDescription) 
      } 
     } 
     }else{ 
      sender.select() 
     } 
    } 

私の脳は...継続する方法がわからないATMをクラッシュされます。私をトラックに連れて戻ってください。

これは私の構造である:

enter image description here

+0

http://stackoverflow.com/a/39492396/6297658 – Dravidian

+1

これは[あなたの前の質問](http://stackoverflow.com/q/39937930)と同じコードです。まったく同じコードで複数の質問を開いていることは、通常、問題を特定するために[MCVE](http://stackoverflow.com/help/mcve)を作成する必要があるという兆候です。 –

+0

質問にはどんなニュースがありますか? –

答えて

2

これが私の機能であると同類を処理するために回避:

class Cell: UITableViewCell { 

    var liked = 0 
    var likes: [String] = [] 
    var likeCounter = 0 

    func readLikeStatus() { 
     if liked == 0 { 
      likeButton.setImage(UIImage(named: "unlike"), forState: .Normal) 
      likeButton.setTitle("", forState: .Normal) 
      likeLabel.text = "\(likeCounter) Likes" 

     } else { 
      likeButton.setImage(UIImage(named: "like"), forState: .Normal) 
      likeButton.setTitle("", forState: .Normal) 
      likeLabel.text = "\(likeCounter) Likes" 
     }   
    } 

    @IBAction func likeButton(sender: AnyObject) { 

     if liked == 0 { 

      likeTweet() 
      liked = 1 
      likeCounter = likeCounter + 1 
      readLikeStatus() 

     } else if liked == 1 { 

      unlikeTweet() 
      liked = 0 
      likeCounter = likeCounter - 1 
      readLikeStatus() 

     } 
    } 

    func likeTweet() { 
     let userID = [backendless.userService.currentUser.objectId: backendless.userService.currentUser.objectId] 
     let usersRef = firebase.child("DeejayTweets").child(passedDJ.objectId).child(tweetID).child("likes") 
     usersRef.updateChildValues(userID) 
    } 

    func unlikeTweet() { 
     let userID = backendless.userService.currentUser.objectId 
     let usersRef = firebase.child("DeejayTweets").child(passedDJ.objectId).child(tweetID).child("likes") 
     usersRef.child(userID).removeValue() 
    } 

    func bindData(post: Tweet, indexPath: NSIndexPath, commentCount: NSMutableDictionary, avatare: NSMutableDictionary) { 
     likeButton.setImage(UIImage(named: "unlike"), forState: .Normal) 
     likeButton.setTitle("", forState: .Normal) 
     liked = 0 

     setLike(post, indexPath: indexPath, commentCount: commentCount, avatare: avatare) 
    } 

    func setLike(post: Tweet, indexPath: NSIndexPath, commentCount: NSMutableDictionary, avatare: NSMutableDictionary) { 

     //SET IF TWEET IS LIKED 
     if post.likes.contains(backendless.userService.currentUser.objectId) { 
      likeButton.setImage(UIImage(named: "like"), forState: .Normal) 
      likeButton.setTitle("", forState: .Normal) 
      liked = 1 
     } 

     //SET LIKE COUNTER 


     if post.likes.count == 1 { 
      likeLabel.text = "0 Likes" 
      likeCounter = 0 
     } else if post.likes.count == 1 { 
      likeLabel.text = "\(post.likes.count - 1) Like" 
      likeCounter = 1 
     } else { 
      likeLabel.text = "\(post.likes.count - 1) Likes" 
      likeCounter = 1 
     } 
    } 

すべてを呼び出すには:

class ViewController: UIViewController.... { 

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 

      let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! Cell 
      let post = tweets[indexPath.row] 

      cell.bindData(post, indexPath: indexPath, commentCount: commentCount, avatare: avatare) 

      return cell 
    } 

そして、私の中にfirebase私はこのようなすべてを扱う:

enter image description here

私は、これは世界のベストプラクティスであると、言っていないんだけど、それが働いていると、あなたにこれを処理する方法のアイデアを与えるかもしれません。

+0

[backendless.userService.currentUser.objectId:backendless.userService.currentUser.objectId]が配列されているのはなぜですか? passedDJ.objectIdの投稿IDは? –

+0

私は価値があり、IDをキーしています。私はそれが好きでした。 uは名前をつけることもできます:id –

+0

イメージ上にこのようなJSON構造体があります。それは鍵になるはずですか? –

関連する問題