【发布时间】:2015-09-16 23:38:05
【问题描述】:
在我的社交媒体应用程序中,我有一个赞按钮...
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 重新加载行时,like 标签也没有改变,这也是我在 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"
有人知道会发生什么吗?谢谢!如果您需要更多信息,请告诉我! (:
【问题讨论】:
-
顺便说一句,您不需要所有这些
dispatch_asyncs 您显示的所有代码都将在主队列上执行
标签: ios iphone swift uitableview parse-platform