我试图制作一个社交媒体应用,其中一个功能就像是按钮,不幸的是,当你点击一切正常但是所有时间它都没有正确更新喜欢的数量和类似按钮正确。我相信它与刷新它有关但我需要刷新它以便它可以更新喜欢的数量。有谁知道发生了什么......
func like(sender: AnyObject) {
var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.table)
var indexPath: NSIndexPath = self.table.indexPathForRowAtPoint(buttonPosition)!
if sender.currentTitle == "Like" {
sender.setTitle("Unlike", forState: .Normal)
var addLikeQuery = PFQuery(className: "Post")
addLikeQuery.whereKey("message", equalTo: self.messages[indexPath.row])
addLikeQuery.findObjectsInBackgroundWithBlock { (aPosts, error) -> Void in
if let aPosts = aPosts {
for aPost in aPosts {
aPost.addUniqueObject(PFUser.currentUser()!.objectId!, forKey: "likers")
self.likeDisplayText = ((aPost["likers"] as! [String]).count - 1).description + " Like"
self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
aPost.saveInBackgroundWithBlock({ (success, error) -> Void in
if error != nil {
self.likeDisplayText = "Couldn't Like Picture!"
}
})
}
}
}
} else {
sender.setTitle("Like", forState: .Normal)
var removeLikeQuery = PFQuery(className: "Post")
removeLikeQuery.whereKey("message", equalTo: self.messages[indexPath.row])
removeLikeQuery.findObjectsInBackgroundWithBlock { (rPosts, error) -> Void in
if let rPosts = rPosts {
for rPost in rPosts {
rPost.removeObject(PFUser.currentUser()!.objectId!, forKey: "likers")
self.likeDisplayText = ((rPost["likers"] as! [String]).count - 1).description + " Like"
self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
rPost.saveInBackgroundWithBlock({ (success, error) -> Void in
if error != nil {
self.likeDisplayText = "Couldn't Like Picture!"
}
})
}
}
}
}
}