在我的社交媒体应用程序我有一个喜欢的按钮…
var likers = [String]()
func like(sender: UIButton) {
var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.table)
var indexPath: NSIndexPath = self.table.indexPathForRowAtPoint(buttonPosition)!
testConnection()
var post = posts[indexPath.row]
if sender.currentTitle == "Unlike" {
dispatch_async(dispatch_get_main_queue()) {
self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
sender.enabled = false
post.removeObject(PFUser.currentUser()!.objectId!, forKey: "likers")
post.saveInBackgroundWithBlock({ (success, error) -> Void in
if success == true {
sender.enabled = true
}
if error != nil {
sender.enabled = true
}
})
} else {
dispatch_async(dispatch_get_main_queue()) {
self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
sender.enabled = false
dispatch_async(dispatch_get_main_queue()) {
post.addUniqueObject(PFUser.currentUser()!.objectId!, forKey: "likers")
post.saveInBackgroundWithBlock({ (success, error) -> Void in
if success == true {
sender.enabled = true
}
if error != nil {
sender.enabled = true
}
})
}
}
}
出于某种原因,当我在indexPath重新加载行,喜欢的标签不改变,这也是我在cellForRowAtIndexPath设置按钮的地方…
dispatch_async(dispatch_get_main_queue()) {
if PFUser.currentUser()?.objectId != nil {
if (post["likers"] as! NSMutableArray).containsObject(PFUser.currentUser()!.objectId!) {
postCellObj.likeButton.setTitle("Unlike", forState: .Normal)
} else {
postCellObj.likeButton.setTitle("Like", forState: .Normal)
}
} else {
postCellObj.likeButton.setTitle("Like", forState: .Normal)
}
}
postCellObj.numberOfLikes.text = ((post["likers"] as! [String]).count - 1).description + " Likes"
有人知道可能发生了什么吗?谢谢!如果你需要更多的信息就告诉我!(:
如果我正确阅读你的代码,你正在删除你的对象后重新加载表格视图单元格,而你正在重新加载你的单元格-标题仍然是"不像",因为你的objectId仍然存在于你的数组:
(1)
if sender.currentTitle == "Unlike" {
dispatch_async(dispatch_get_main_queue()) {
self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .Automatic)
}
...
(2)
if (post["likers"] as! NSMutableArray).containsObject(PFUser.currentUser()!.objectId!) {
postCellObj.likeButton.setTitle("Unlike", forState: .Normal)
}
(3)
...
post.removeObject(PFUser.currentUser()!.objectId!, forKey: "likers")
我可以通过将它放入方法
来解决这个问题if success == true {
}
通过
sender.title = "Like" //Or unlike for the else statement
当我大声说出来的时候,我会接受这个答案但是如果有人知道怎么做,我很想听到如何停止
的恶心动画table.reloadRowsAtIndexPaths([indexPath], withRoAnimation: .None)
因为即使有。none,它看起来还是不那么好。到目前为止,我能找到的唯一答案是刷新整个表,但我不想这么做。