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

add button to uitableviewcell programmatically swift
uitableviewcell button click swift 4
button click in uitableviewcell swift
uitableviewcell button action objective-c
get button click inside uitableviewcell swift 4
uibutton in uitableviewcell
how to properly do buttons in table view cells using swift closures
swift uitableviewcell tag
 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.

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)
                    }

                }

how to change the button title on click inside a tableview cell in swift 3, First of all you should take one Array for manage clicked/changed UIButton for reusableCell otherwise if you scroll your tableView Up-Down  Tag: ios,image,swift,button I’m trying to change the image of a button when tapped (it’s for an audio player that I want Play to change to Pause). So far I’ve only managed to change it when the button is held, then it changes back when released.

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..!

UIButton in UITableViewCell using Delegates and Protocols in Swift , 75 thank you so much, YouTube! Read more. Show less. Reply 3 Duration: 9:54 Posted: Apr 12, 2017 Question: Tag: ios,uitableview,swift,ios8 I am able to display a list of text (one piece of text in each row) in UITableView in iOS using Swift. My question is - what if I wanted to add more UI Elements in each row - like a button, image, etc.

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
}

Make button clickable in tableView cell using Protocol in Xcode9.0 , In this video I am explaining how we can use protocol to make button Please watch: "Create a Duration: 18:40 Posted: Jan 27, 2018 In this video, I show you how you can turn your table view cells into buttons, and make them trigger a certain action when your user tapps on them. This tutorial was done in Xcode 8, using Swift 3

iOS UITableView Custom Cell with Image, with example. In ios we can customize tableview cell to show images in uitableview. Once you finished entering all the options then click on the Next button like as shown below. ios ios tableview change prototype cells in xcode. Now we  Swift UITableView Tutorial with Custom Cells - Beginner Series - Duration: Sizing UITableView Cells to Fit Images! (Swift in Xcode) - Duration: 9:50. Archetapp 19,649 views.

change button image when play completes swift, Try to set background image for button let image = UIImage(named: "play.png") as UIImage how to change button background colour in tableview cell swift3. Today we're gonna dive into creating custom table view cells using all code baby! Hope you guys enjoy it! :D Music by Andrew ApplePie Jarvis Desk - https://g

Uitableview edit mode custom delete button swift 4, We detail how to do Upon touching the Edit button, the table will go into “editing mode”. We'll do this Obviously, in order to show different images, we need to alter this line of code. Oct 28, 2017 · UITableView with Custom Cell (Swift 4 + Xcode 9. Insert 3 items at the end, then delete 3 items at If you've followed the iOS  In the Attributes Inspector, change it’s Style to “Basic”, and give it the Identifier “TextCell”. Open the Assistant Editor, and Ctrl+Drag from your UITableView to somewhere in your code. We are making an outlet for the UITableView in our ViewController code, so that we can access it to modify things.

Comments
  • When a button scrolls off the the screen it is saved and reused when another cell scrolls into view. Cell content is not reset automatically so you have to set the default image when you create the cell in cellForRowAt method.
  • Thank you! I added a line; doLikeBtn.setImage(UIImage(named: "btn_ok"), for: UIControlState.normal) So other button images are not changed. But the target button image is changed to original image after scrolling. Could you help me more please?
  • just I am update the code, please make sure to keep track of the state of your cell "like status" it's one of best practises
  • Thank a lot! It's very useful for me! I am developing some swift apps, please help me in the future...