在我的社交媒體應用程序我有一個類似按鈕...刷新行不工作
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"
有誰知道可能會發生什麼?謝謝!只要告訴我你是否需要更多信息! (:做
sender.title = "Like" //Or unlike for the else statement
我要接受這個當我一個答案
另外,您不需要所有這些'dispatch_async's您顯示的所有代碼都將在主隊列中執行 – Paulw11