Ios 好像按钮不是每次都能用

Ios 好像按钮不是每次都能用,ios,iphone,xcode,swift,parse-platform,Ios,Iphone,Xcode,Swift,Parse Platform,我正在尝试制作一个社交媒体应用程序,其中一个功能是“喜欢”按钮,不幸的是,当你点击“一切”按钮时,效果很好,但它始终无法正确更新“喜欢”和“喜欢”按钮的数量。我相信这和刷新它有关,但我需要刷新它,这样它才能更新喜欢的数量。有人知道发生了什么吗 func like(sender: AnyObject) { var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.table) var

我正在尝试制作一个社交媒体应用程序,其中一个功能是“喜欢”按钮,不幸的是,当你点击“一切”按钮时,效果很好,但它始终无法正确更新“喜欢”和“喜欢”按钮的数量。我相信这和刷新它有关,但我需要刷新它,这样它才能更新喜欢的数量。有人知道发生了什么吗

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!"

                        }
                    })

                }
            }
        }
    }
}

我不确定你的问题是什么,但我可能有一些见解。如果您试图通过一个执行大量处理的函数或位于另一个线程中的函数来编辑UI中的某些元素,我建议将其设置为异步,并在该函数中编辑UI的任何位置使代码在主线程上运行。这就是我的意思

func like(sender: AnyObject) { 
  dispatch_async(dispatch_get_global_queue(Int(QOS_CLASS_USER_INITIATED.value), 0)) {
    // do background stuff

    dispatch_async(dispatch_get_main_queue()) {
      // when you want to edit the text of the button
      // or change something in your UI
      // do it in here
      self.likeDisplayText = "Couldn't Like Picture!"
    }
  }
}

我试过了,但没用。我不确定发生了什么,但我的“喜欢”按钮根本不起作用。使用Xcode的调试器,逐行运行函数,看看有什么问题。我不认为我只是觉得我做错了什么。你知道如何制作like按钮的教程吗?你真的应该学习如何在Xcode上使用调试器。它允许您选择代码中停止程序的位置,并允许您逐行查看所有变量,看看是什么弄乱了程序。除了那个建议,我真的帮不了你。祝你好运,我的朋友: