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

8
  • stackoverflow.com/a/39492396/6297658 Commented Oct 9, 2016 at 12:29
  • 1
    This is the same code as in [your previous question]( stackoverflow.com/q/39937930). Having multiple questions open with the exact same code is typically a sign that you should create a MCVE to isolate the problem. Commented Oct 9, 2016 at 17:30
  • dude any news on your question? Commented Oct 12, 2016 at 16:26
  • I actually were stuck with indexPath.rows because I had to use those outside of tableview functions so I gave up. But I have plan to read your question and accept it today :) Commented Oct 12, 2016 at 16:31
  • ooooookay and how did that work out? :P Commented Oct 13, 2016 at 17:33

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

21 Comments

Can you say, why [backendless.userService.currentUser.objectId: backendless.userService.currentUser.objectId] is in array? And is the passedDJ.objectId post id?
so i have value and key the id, i wanted it like that. u could also put name:id
I have this JSON struct like on image. So it should be key?
all in needed for my likes is to save the "liker" as array of [key:id and value:id] that way i can access it the fastest. i can count the amount of likers and i can have a where clause to get all the users for ids by "allvalues"
May I ask you one thing. Why you did struct like this for posts? Like storing post inside UID? Is there some purpose for later on?
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.