tableview cell UIButton

I have a tableview that in each row has two buttons. If tapped on, the button background color changes.The issue is when scrolling down the table view. A Button background color has changed, that I did not even tapped on. So what I can understand is that, If i have more data to be displayed then the index is replaced by another index when i scroll because

of reusablecell. I found that tags are good way to go about it, but the tutorial and suloting I have found weren't the best. Can some explain to me how I get only that button background color to change. When one of the two button's is selected in that row and not another row.

viewcontroller

import UIKit

class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
    
    
    
    var myArray:[String] = ["my", "by", "hello", "run", "Was", "sie", "very", "hungry", "code", "monkey", "dew", "big", "warm", "fuzzy", "hart", "I", "have", "every", "reason", "waiting", "some", "day", "simple", "man", "xcode"]
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let silly = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
        
        silly.textLabel?.text = myArray[indexPath.row]
        
        silly.fail.tag = indexPath.row
        
        silly.fail.addTarget(self, action: "failed", for: .touchUpInside)
        
        return silly
    }
    
    func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}


tableviewcell

import UIKit

class CellTableViewCell: UITableViewCell {

    @IBOutlet weak var fail: UIButton!
    @IBOutlet weak var pass: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    @IBAction func okBtn(_ sender: Any) {
        pass.backgroundColor? = UIColor.green
    }
    
    @IBAction func errorBtn(_ sender: Any) {
        fail.backgroundColor? = UIColor.red
    }
}

The problem is that you don't unset the old target when you reconfigure the cell in line 24. Because cells can be reused, the cell that's been dequeued may be one that you've previously set a target on. You can remove the old target(s) like this:


     silly.fail.removeTarget(nil, action: nil, for: .touchUpInside)

@QuineceyMorris

No luck. Nothing happens when a button is touched

here a link to what I'm practicing on. https://github.com/morenoa/iOS3/blob/master/Table%20Cell.xcodeproj.zip

import UIKit

class ViewController: UIViewController,UITableViewDataSource, UITableViewDelegate {
    
    
    
    var myArray:[String] = ["my", "by", "hello", "run", "Was", "sie", "very", "hungry", "code", "monkey", "dew", "big", "warm", "fuzzy", "hart", "I", "have", "every", "reason", "waiting", "some", "day", "simple", "man", "xcode"]
    
    let fun = CellTableViewCell()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return myArray.count
    }
    
    internal func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let silly = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath) as! CellTableViewCell
        
        silly.textLabel?.text = myArray[indexPath.row]
        
        silly.fail.tag = indexPath.row
        
        silly.fail.addTarget(self, action: #selector(ViewController.errorBtn(_:)), for: .touchUpInside)
        //silly.fail.removeTarget(nil, action: nil, for: .touchUpInside)
        return silly
    }
    
    func tableView(_ tableView: UITableView, accessoryButtonTappedForRowWith indexPath: IndexPath) {
        
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func errorBtn(_ sender: UIButton) {
        let tittle = myArray[(sender as AnyObject).tag]
        print(tittle)
        
        sender.backgroundColor = UIColor.red
        
    }
    

}


import UIKit

class CellTableViewCell: UITableViewCell {

    @IBOutlet weak var fail: UIButton!
    @IBOutlet weak var pass: UIButton!
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}
Accepted Answer

You need to do line 27 before line 26. Otherwise, the "removeTarget" removes the target you just added.

tableview cell UIButton
 
 
Q