LoveCode
LoveCode

Reputation: 69

Swift 3: how to change the button image in UITableView?

 func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

   ...

 let doLikeBtn = detailCell.contentView.viewWithTag(106) as! UIButton
                doLikeBtn.setTitle(String(indexPath.row), for: UIControlState.normal)
                doLikeBtn.addTarget(self, action: #selector(doLike(sender:)), for: UIControlEvents.touchUpInside)

...

 }

...

 func doLike(sender: UIButton) {
        selectUserIndex = sender.title(for: UIControlState.normal)!

    if sender.image(for: UIControlState.normal) == UIImage(named: "btn_Like.png") {
        sender.setImage(UIImage(named: "btn_not_Like.png"), for: UIControlState.normal)
    }else {
        sender.setImage(UIImage(named: "btn_Like.png"), for: UIControlState.normal)
    }
 }

If I click one button in table, and then change the image of button, but other some button's images also are changed repeatedly. I can't understand this. Please help me! Best regards.

Upvotes: 1

Views: 2703

Answers (3)

Mohan Ramanathan
Mohan Ramanathan

Reputation: 2209

In Swift 4, Below is a simple way to do it

class AddressTableViewCell: UITableViewCell {
    @IBOutlet weak var starButton: UIButton!
    var buttonObject : (() -> Void)? = nil
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    @IBAction func buttonPressed(sender: UIButton) {
        if let buttonAction = self.buttonObject {
            buttonAction()
        }
    }
}

 public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell{
    let cell = tableView.dequeueReusableCell(withIdentifier: "addressTableViewCell", for: indexPath) as! AddressTableViewCell
    cell.buttonObject = {
        if cell.starButton.currentImage == UIImage(named: "fav") {
            cell.starButton.setImage(UIImage(named: "favYellow"), for: .normal)
        } else {
            cell.starButton.setImage(UIImage(named: "fav"), for: .normal)
        }
    }
    return cell
}

Upvotes: 0

Kiran Jadhav
Kiran Jadhav

Reputation: 3327

Updated for Swift 3/4:

Use lines of code to change/set the UIButton Image in UITableView;

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "YourTableViewCellStoryboardIdentifier")! as! YourTableViewCellName

        if status == 1{
            cell.mStatusButtonOutlet.setImage(UIImage(named: "pending_icon"), for: .normal)
        }else if status == 2{
            cell.mStatusButtonOutlet.setImage(UIImage(named: "approval"), for: .normal)
        }else if status == 3{
            cell.mStatusButtonOutlet.setImage(UIImage(named: "reject_icon"), for: .normal)
        }else{
            cell.mStatusButtonOutlet.setImage(UIImage(named: "cancle"), for: .normal)
        }

    return cell
}

Note: where mStatusButtonOutlet - is your UIButtonOutlet

Enjoy..!

Upvotes: 0

Yahya Alshaar
Yahya Alshaar

Reputation: 163

a UITableViewCell object is reusable—that is for performance reasons, you should only reset attributes of the cell that are not related to content.

So you need to update your cell every time cellForRowAt invoked:

also you need to keep track of which cell getting "liked or dislike" so you can add for example a boolean isLike property to YourCustomCell and toggle it true or false upon doLike func triggered

            func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
                let cell = tableView.deque... as! YourCustomCell

                if cell.isLiked == false {
                        cell.btnSomething.setImage(UIImage(named: "btn_not_Like.png"), for: UIControlState.normal)
                    }else {
                        cell.btnSomething.setImage(UIImage(named: "btn_Like.png"), for: UIControlState.normal)
                    }

                }

Upvotes: 1

Related Questions