0

I have two classes TableViewController and CustomCell then connect the button(likeBtn) in IB to CustomCell.

And now, I wanna to change the image of this button!

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cellIdentifier = "Cell"

        let cell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath) as! CustomCell

        // Configure the cell...            
        cell.likeBtn.addTarget(self, action: #selector(TableViewController.liked), for: .touchUpInside)

        return cell
    }

@objc func liked(sender: UIButton) {

        cell.likeBtn.setImage(UIImage(named: "liked.png"), for: .normal)
        cell.likeBtn.isUserInteractionEnabled = false
    }

But in the liked(), I've errors about cell.likeBtn.

I'm programming with swift 4

0

1 Answer 1

1

The correct way should be:

@objc func liked(sender: UIButton) {
    sender.setImage(UIImage(named: "liked.png"), for: .normal)
    sender.isUserInteractionEnabled = false
}
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, thanks, I thought about sender.tag or pass parameter, lol. And now I encounter to a BUG that is about when clicked on first cell, some cells is clicked in table!!
If you only want certain cell button to be clicked, just do this in cellForRowAt: if indexPath.row == (which row) {cell.likeBtn.addTarget(self, action: #selector(TableViewController.liked), for: .touchUpInside)}
thx, but I want click on any cell in the table and saves this for only I clicked!
you can always add tag, your tag can be indexPath.row so that every button has a unique tag

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.