Swift - 在 UITableView 中向 UIButton 添加目标不起作用

问题描述

我有一个UITableView,在每个UITableViewCells中,我有一个按钮,应该在选择时执行动作。如果用户决定选择实际单元格,我会在 didSelectRowAt 函数中处理另一个操作。我不确定我是否遗漏了什么,但我的按钮在被选中时不起作用,但执行了 didSelectRowAt 功能

布局和约束工作得很好。它只是无法识别点击,然后推送到 didSelectRowAt

UIViewController 代码(已剪辑)

class MyProfile: UIViewController,UITableViewDelegate,UITableViewDataSource {
    
    let tableView : UITableView = {
        let tableView = UITableView()
        tableView.translatesAutoresizingMaskIntoConstraints = false
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor.white
        return tableView
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.addSubview(tableView)
        tableView.topAnchor.constraint(equalTo: self.view.topAnchor,constant: 10).isActive = true
        tableView.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
        tableView.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        tableView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor,constant: 10).isActive = true
        
        view.backgroundColor = UIColor.white
        
        tableView.delegate = self
        tableView.dataSource = self
        tableView.register(ProfileCell.self,forCellReuseIdentifier: "profileCell")

        // data and backend stuff to reload tableview has been removed,so there is not too much code
        // Do any additional setup after loading the view.
    }
    
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView,numberOfRowsInSection section: Int) -> Int {
        self.myServices.text = "My Services"
        return myJobs.count
    }
    
    func tableView(_ tableView: UITableView,cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "profileCell",for: indexPath) as! ProfileCell
        
        cell.selectionStyle = .none
        
        cell.deleteButton.addTarget(self,action: #selector(deletePostpressed),for: .touchUpInside)
        cell.editButton.addTarget(self,action: #selector(editPostpressed),for: .touchUpInside)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView,didSelectRowAt indexPath: IndexPath) {
        self.navigationController?.pushViewController(ViewPostController(),animated: true)
    }
    
    func tableView(_ tableView: UITableView,heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 175
    }
    
    @objc func deletePostpressed() {
        print("delete")
    }
    
    @objc func editPostpressed() {
        print("edit")
        self.present(Home(),animated: true,completion: nil)
    }

}

UITableViewCell 代码(已剪辑)

class ProfileCell: UITableViewCell {
    
    let editButton : UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Edit",for: .normal)
        button.setTitleColor(UIColor.mainBlue,for: .normal)
        button.backgroundColor = UIColor(red: 240/255,green: 240/255,blue: 240/255,alpha: 1)
        button.isUserInteractionEnabled = true
        button.translatesAutoresizingMaskIntoConstraints = false
        button.layer.borderWidth = 1
        button.layer.borderColor = UIColor.mainBlue.cgColor
        return button
    }()
    
    let deleteButton : UIButton = {
        let button = UIButton(type: .system)
        button.setTitle("Delete",alpha: 1)
        button.isUserInteractionEnabled = true
        button.translatesAutoresizingMaskIntoConstraints = false
        button.layer.borderWidth = 1
        button.layer.borderColor = UIColor.mainBlue.cgColor
        return button
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
        self.selectionStyle = .none
    }
    
    override init(style: UITableViewCell.CellStyle,reuseIdentifier: String?) {
        super.init(style: style,reuseIdentifier: reuseIdentifier)
        
        isUserInteractionEnabled = true
        
        addSubview(editButton)
        editButton.layer.cornerRadius = 15
        editButton.layer.masksToBounds = true
        editButton.topAnchor.constraint(equalTo: saleNumber.bottomAnchor,constant: 7).isActive = true
        editButton.rightAnchor.constraint(equalTo: informationView.rightAnchor,constant: -15).isActive = true
        editButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
        editButton.widthAnchor.constraint(equalToConstant: 85).isActive = true

        addSubview(deleteButton)
        deleteButton.layer.cornerRadius = 15
        deleteButton.layer.masksToBounds = true
        deleteButton.topAnchor.constraint(equalTo: saleNumber.bottomAnchor,constant: 7).isActive = true
        deleteButton.rightAnchor.constraint(equalTo: editButton.leftAnchor,constant: -15).isActive = true
        deleteButton.heightAnchor.constraint(equalToConstant: 30).isActive = true
        deleteButton.widthAnchor.constraint(equalToConstant: 85).isActive = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

我不确定还有什么问题。我已经声明了委托,更正了所有内容,并确保需要初始化的其他所有内容都已完成,但仍然无法正常工作。

解决方法

这是一个层次问题,您需要将按钮添加到 contentView。如果您只是将 subview 添加到 view,它会在 contentViewUITableViewCell 后面。

视觉上,选中的那个是单元格的contentView,你可以看到实际的按钮在它的后面: enter image description here

所以 contentView.addSubview(editButton)contentView.addSubview(deleteButton) 完成了这项工作。