我在tableview单元格中创建了类似系统。但它有问题。
这就像按钮系统:
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
}
}
这就是我的喜欢系统功能:
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 ..不知道如何继续。请带我回到赛道。
这是我的结构:
答案 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
}
在我的火力基础中,我处理这样的事情:
我不是说,这是世界上最好的做法,但它有效,可能会让你知道如何处理这个问题。