1

I have a UITableView in which I have custom cells ("WishCell"). Inside of that cell is a UIButton but somehow the linked func checkButtonTapped is not working. It doesn't even print... I have no idea why this code is not working:

    class WhishCell: UITableViewCell {
    
    let label: UILabel = {
       let v = UILabel()
        v.font = UIFont(name: "AvenirNext", size: 23)
        v.textColor = .white
        v.font = v.font.withSize(23)
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()
    
        let checkButton: UIButton =  {
        let v = UIButton()
        v.backgroundColor = .darkGray
//        v.layer.borderColor = UIColor.red.cgColor
//        v.layer.borderWidth = 2.0
        v.translatesAutoresizingMaskIntoConstraints = false
        v.setBackgroundImage(UIImage(named: "boxUnchecked"), for: .normal)
        v.addTarget(self, action: #selector(checkButtonTapped), for: .touchUpInside)
        return v
    }()
    
    
    
    public static let reuseID = "WhishCell"

    required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.backgroundColor = .clear
        
        // add checkButton
        self.contentView.addSubview(checkButton)
        self.checkButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
        self.checkButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.checkButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
        self.checkButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
        
        // add label
        self.contentView.addSubview(label)
        self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 60).isActive = true
        self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        
        
        
    }
    
    @objc func checkButtonTapped(){
        print("hi")
        self.checkButton.setBackgroundImage(UIImage(named: "boxChecked"), for: .normal)
        self.checkButton.alpha = 0
        self.checkButton.transform =  CGAffineTransform(scaleX: 1.3, y: 1.3)
        
        UIView.animate(withDuration: 0.3) {
            self.checkButton.alpha = 1
            self.checkButton.transform = CGAffineTransform.identity
        }
    }
}

What am I doing wrong?

4
  • can you check the view hierarchy? I think that the label is over the button Commented Nov 26, 2019 at 21:16
  • @AndresGomez nope just checked it. It even does the "click"-animation Commented Nov 26, 2019 at 21:19
  • First, don't call your variables with single letters like "v", be more informative. Second, it is better to use the delegation pattern. In the table view controller declared the class that will be the delegate for this cell and call the delegate method in the button event. Are you using storyboards? it will make life easier and save you coding time, like adding buttons manually. Commented Nov 26, 2019 at 21:25
  • sorry for the variable name. Is anything not clear? And in my case doing this programmatically is actually easier :) Do you have any idea why it is not working? Commented Nov 26, 2019 at 21:35

1 Answer 1

1

Try with this

class WhishCell: UITableViewCell {

    let label: UILabel = {
        let v = UILabel()
        v.font = UIFont(name: "AvenirNext", size: 23)
        v.textColor = .black
        v.font = v.font.withSize(23)
        v.translatesAutoresizingMaskIntoConstraints = false
        return v
    }()

    let checkButton: UIButton =  {
        let v = UIButton()
        v.backgroundColor = .blue
        //        v.layer.borderColor = UIColor.red.cgColor
        //        v.layer.borderWidth = 2.0
        v.clipsToBounds = true
        v.translatesAutoresizingMaskIntoConstraints = false
        v.setBackgroundImage(UIImage(named: "boxUnchecked"), for: .normal)
        return v
    }()

    public static let reuseID = "WhishCell"
    required init?(coder: NSCoder) {fatalError("init(coder:) has not been implemented")}

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)

        self.backgroundColor = .clear

        // add checkButton
        self.contentView.addSubview(checkButton)
        self.checkButton.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 20).isActive = true
        self.checkButton.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
        self.checkButton.widthAnchor.constraint(equalToConstant: 40).isActive = true
        self.checkButton.heightAnchor.constraint(equalToConstant: 40).isActive = true
        self.checkButton.addTarget(self, action: #selector(checkButtonTapped), for: .touchUpInside)

        // add label
        self.contentView.addSubview(label)
        self.label.leadingAnchor.constraint(equalTo: self.leadingAnchor, constant: 60).isActive = true
        self.label.centerYAnchor.constraint(equalTo: self.centerYAnchor).isActive = true
    }

    @objc func checkButtonTapped() {
        print("hi")
        self.checkButton.setBackgroundImage(UIImage(named: "boxChecked"), for: .normal)
        self.checkButton.alpha = 0
        self.checkButton.transform =  CGAffineTransform(scaleX: 1.3, y: 1.3)

        UIView.animate(withDuration: 0.3) {
            self.checkButton.alpha = 1
            self.checkButton.transform = CGAffineTransform.identity
        }
    }
}

Sign up to request clarification or add additional context in comments.

2 Comments

it works! Why does it only work if you addTarget afterwards?
It works because your target is the cell and not the button itself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.