【问题标题】:Swift - Adding target to UIButton in UITableView not workingSwift - 在 UITableView 中向 UIButton 添加目标不起作用
【发布时间】:2021-04-22 23:16:00
【问题描述】:

我有一个 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", 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
    }()
    
    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")
    }
}

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

【问题讨论】:

    标签: ios swift uitableview uikit didselectrowatindexpath


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2011-06-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多