I created like system into my tableview cell. However it has problems.
- If I like one thing, every 7th cell is getting like also, why? Is there something with reusableCell?
- 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: