John Doe
John Doe

Reputation: 1609

Reload row at indexpath not working

In my social media app I have a like button...

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
                }

            })

        }
    }

}

For some reason when I reload rows at indexPath the like label doesn't change also this is where I set the buttons in 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"

Does anyone know what may be going on? Thanks! And just tell me if you need more information! (:

Upvotes: 0

Views: 1010

Answers (2)

Valentin
Valentin

Reputation: 3302

If I am reading your code correctly, you are removing your object after reloading the table view cell, while you are reloading your cell - the title remains "Unlike" as your objectId is still present in your array:

(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")

Try breakpointing it.

Upvotes: 1

John Doe
John Doe

Reputation: 1609

I was able to solve the problem by putting it in the method

if success == true { 
}

And by doing

sender.title = "Like" //Or unlike for the else statement

I'm going to accept this as an answer when I'm aloud to but if anyone knows how, I would love to hear how to stop that gross animation for

table.reloadRowsAtIndexPaths([indexPath], withRoAnimation: .None)

because even with the .None it still doesn't look that great. So far the only answer I've been able to find is by refreshing the whole table but I don't want to do that.

Upvotes: 0

Related Questions