1

I created like system into my tableview cell. However it has problems.

  1. If I like one thing, every 7th cell is getting like also, why? Is there something with reusableCell?
  2. What is the best approach doing it, am I doing it totally wrong?

This is the like button system:

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 
            }
        }

And this is my likeSystem function:

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()
        }
    }

My brain is crashing ATM.. Do not know how to continue. Please lead me back to the track.

This is my Structure:

enter image description here

Tarvo Mäesepp
  • 4,477
  • 3
  • 44
  • 92

1 Answers1

2

This is my functions and work around to handle likes:

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
        }
     }

To call everything:

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
    }

And in my firebase I handle everything like this:

enter image description here

I'm not saying, that this is the world's best practise, but it is working and might give you the idea of how to handle this.

David Seek
  • 16,783
  • 19
  • 105
  • 136