按钮标题被单元格刷新覆盖

时间:2015-09-14 00:21:45

标签: ios swift parse-platform

当使用这行代码单击按钮时,我正在刷新一个单元格...

self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)

由于某些原因,您有时会单击该按钮将其还原为故事板中的原始标题。这是整个按钮功能,万一这有助于......

 var liked = [Bool]()

func like(sender: AnyObject) {

    var buttonPosition: CGPoint = sender.convertPoint(CGPointZero, toView: self.table)

    var indexPath: NSIndexPath = self.table.indexPathForRowAtPoint(buttonPosition)!

    if liked[indexPath.row] == true {

        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")
                    rPost.saveInBackgroundWithBlock({ (b, e) -> Void in
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
                            sender.setTitle("Unlike", forState: .Normal)
                        })
                    })

                }
            }
        }

        self.liked[indexPath.row] = false

    } else {

        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")
                    aPost.saveInBackgroundWithBlock({ (b, e) -> Void in
                        dispatch_async(dispatch_get_main_queue(), { () -> Void in
                            self.table.reloadRowsAtIndexPaths([indexPath], withRowAnimation: .None)
                            sender.setTitle("Like", forState: .Normal)

                        })
                    })


                }
            }
        }

        self.liked[indexPath.row] = true

    }

    println("liked = \(liked)")


}

在cellForRowAtIndexPath我有......

       var postsQuery = PFQuery(className: "Post")

        postsQuery.whereKey("message", equalTo: self.messages[indexPath.row])

        postsQuery.findObjectsInBackgroundWithBlock { (posts, error) -> Void in
            if let posts = posts {
                for post in posts {

                    dispatch_async(dispatch_get_main_queue(), { () -> Void in

                        if self.liked.count < self.messages.count {
                            if (post["likers"] as! NSMutableArray).containsObject(PFUser.currentUser()!.objectId!) {
                                self.liked.append(true)
                                postCellObj.likeButton.setTitle("Unlike", forState: .Normal)

                            } else {
                                self.liked.append(false)
                            }
                        }

                        postCellObj.numberOfLikes.text = (post["likers"] as! [String]).count.description + " Likes"

                    })
                }
            }
        }

1 个答案:

答案 0 :(得分:0)

我认为当按钮未更改时,大小写属于最后一个else块。

if showingMore[indexPath.row] {
        postCellObj.message.text = messages[indexPath.row]
        postCellObj.showAllAndLessButton.setTitle("Show Less", forState: .Normal)
        postCellObj.showAllAndLessButton.hidden = false

    }

    else if count(messageString) >= 800 {
        var messageNs = messageString as NSString
        var messageFinal = messageNs.substringWithRange(NSRange(location: 0, length: 800))
        postCellObj.message.text = messageFinal as String + "..."
        postCellObj.showAllAndLessButton.setTitle("Show All", forState: .Normal)
        postCellObj.showAllAndLessButton.hidden = false

    } else {
        postCellObj.message.text = messages[indexPath.row]
        // I think when the button is not changed, the case is belong to here
    }