jeudi 25 juin 2015

Cell reuse beginner? Tapping button in one cell affects another

I have a TableViewController with a custom cell. When I tap the like button inside one of the cells, it is causing at least one other cell to have the like button tapped.

I am using Parse, and it is not affecting the actual like count of the second one which is being ghost tapped, but it is disabling the like button and turning it red.

I have read about cell reuse, and similar topics but am completely lost. I am new to swift, and if someone could help me navigate how to fix this, I can't find a solution on SO about Swift, and Parse.

TableViewController

 @IBAction func likeButtonTapped(sender: AnyObject) {

    let chinTwo = self.timelineData[sender.tag] as! PFObject

    chinTwo["likeCount"] = (chinTwo["likeCount"] as! Int) + 1

    sender.setTitleColor(UIColor.redColor(), forState: UIControlState.Normal)

    chinTwo.saveInBackgroundWithBlock {
        (success: Bool, error: NSError?) -> Void in
        if (success) {
            println("Worked")
        } else {
            println("Didn't Work")
        }
    }

    self.tableView.reloadData()

}

TableViewCell

@IBAction func likeTapped(sender: AnyObject) { 
    likeButton.enabled = false
}

The same problem occurs with the report button.

See more ..

Aucun commentaire:

Enregistrer un commentaire