ios - Swift - 在 UITableViewCell 中添加 buttonTapped 函数

标签 ios swift uitableview

我有一个 UITableView,其中有自定义 cells(“WishCell”)。该 cell 内部是一个 UIButton,但不知何故,链接的 func checkButtonTapped 无法正常工作。它甚至不 print...我不知道为什么这段代码不起作用:

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

我做错了什么?

最佳答案

试试这个

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

关于ios - Swift - 在 UITableViewCell 中添加 buttonTapped 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59059353/

相关文章:

core-data - Swift - 将支持 iCloud 的持久存储添加到核心数据

ios - 我想要动态获取 uitableview 总高度,而不是 uitableviewcell,

ios - 如何在 iphone 中的选定单元格上显示选择箭头图像

ios - Swift - 使用运行时参数的选择器的补丁定义

ios - 避免在 View 中再次获取图像

ios - FMDB更新版本号不起作用

ios - 如何使用 RxSwift 观察 UITextField 中的文本变化?

iphone - 更改分组表边框半径

objective-c - 简单的Objective-C脚本打开一个URL

ios - Objective c 只允许两个单词之间有空格,前后都不允许