Reputation: 1614
I have a tableView in which i post users feeds and i added a heart button for like in the cell view. I created a class for the cell view and declared my @IBOutlet
of the button there. Then in the cellForRowAtIndexPath
in the tableview i called the button and made the indexpath.row
the tag number of the button itself. Then i added a target with an action to be done and created my @IBAction
. Now I'm trying to change the image look of the heart button to red but nothing happens. Is there a problem passing the an UIImage to the button via the sender. I have no errors. And the if like =
statement is working correctly. Here is my code:
override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as! PostsCellTableViewCell
cell.heartButton.tag = indexPath.row
cell.heartButton.addTarget(self, action: "liked:", forControlEvents: .TouchUpInside)
return cell
}
@IBAction func liked (sender: UIButton){
if like == false{
sender.imageView?.image = UIImage(contentsOfFile: "red-heart.png")
like = true
}
else{
sender.imageView?.image = UIImage(contentsOfFile: "white-heart-hi.png")
like = false
}
// self.tableView.reloadData()
}
Upvotes: 0
Views: 70
Reputation: 170
Try this instead.
if like == false{
sender.setImage(UIImage(named: "red-heart.png"), forState: .Normal)
like = true
} else {
sender.setImage.setImage(UIImage(named: "white-heart-hi.png"), forState: .Normal)
like = false
}
Upvotes: 2
Reputation: 143
This is a really simple error. You declared your target function as "liked:", but your function is declared as "liked". Notice the lack of a colon the second time. This is critical, and should fix the problem. If this does not fix the problem, make sure you have declared
var like = false
If this still doesn't work, comment and I will continue helping. If it does work, don't forget to check my answer.
Upvotes: -1