简体   繁体   English

TableViewCell 中 UIButton 的 addTarget 不起作用

[英]addTarget for UIButton in TableViewCell doesn't work

I'm making a table view app, and have created UITableViewCell programmatically.我正在制作一个表格视图应用程序,并以编程方式创建了 UITableViewCell。 The cell consists of 2 views, the first one for the Label, and the second one is for 2 buttons and label.该单元包含 2 个视图,第一个用于 Label,第二个用于 2 个按钮和 label。 Pressing on the button should change the label's title.按下按钮应更改标签的标题。 Also, I'm using delegates to implement logic in the view controller.此外,我正在使用委托在视图 controller 中实现逻辑。 Here is my cell code.这是我的手机密码。 Sorry for the mess with the constraints.对不起,约束混乱。 I'm only learning to implement them nicely.我只是在学习如何很好地实现它们。

protocol FinalCellDelegate {
    func addButtonPressed(sender: UIButton)
    func subtactButtonPressed(sender: UIButton)
}


class FinalCell: UITableViewCell {
  let label = UILabel()
  var addButton = UIButton()
  var numberLabel = UILabel()
  var subtractButton = UIButton()
  var delegate: FinalCellDelegate?


  override func awakeFromNib() {
       super.awakeFromNib()

   }

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


        contentView.addSubview(label)
        let addAndSubtractView = UIView()
        addAndSubtractView.addSubview(addButton)
        addAndSubtractView.addSubview(numberLabel)
        addAndSubtractView.addSubview(subtractButton)
        contentView.addSubview(addAndSubtractView)

        //constraints
        addButton.translatesAutoresizingMaskIntoConstraints = false
        addButton.trailingAnchor.constraint(equalTo: addAndSubtractView.trailingAnchor, constant: -15).isActive = true
        addButton.leadingAnchor.constraint(equalTo: numberLabel.trailingAnchor, constant: 10).isActive = true
        addButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        subtractButton.translatesAutoresizingMaskIntoConstraints = false
        subtractButton.leadingAnchor.constraint(equalTo: addAndSubtractView.leadingAnchor, constant: 10).isActive = true
        subtractButton.trailingAnchor.constraint(equalTo: numberLabel.leadingAnchor, constant: -10).isActive = true
        subtractButton.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        numberLabel.translatesAutoresizingMaskIntoConstraints = false
        numberLabel.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        addAndSubtractView.translatesAutoresizingMaskIntoConstraints = false
        addAndSubtractView.trailingAnchor.constraint(equalTo: contentView.trailingAnchor, constant: -5).isActive = true
        addAndSubtractView.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true
        label.translatesAutoresizingMaskIntoConstraints = false
        label.leadingAnchor.constraint(equalTo: contentView.leadingAnchor, constant: 20).isActive = true
        label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor).isActive = true


        label.textColor = .black
        subtractButton.setTitle("-", for: .normal)
        subtractButton.setTitleColor(.black, for: .normal)
        addButton.setTitle("+", for: .normal)
        addButton.setTitleColor(.black, for: .normal)
        numberLabel.text = "0"
        numberLabel.textColor = .black

        subtractButton.addTarget(self, action: #selector(subtractButtonTapped(_:)), for: .touchUpInside)
        addButton.addTarget(self, action: #selector(addButtonTapped(_:)), for: .touchUpInside)
}

  @objc func addButtonTapped(_ sender: UIButton) {
      print("addButtonTapped")
       delegate?.addButtonPressed(sender: sender)
   }

   @objc func subtractButtonTapped(_ sender: UIButton) {
       print("subtractButtonTapped")
       delegate?.subtactButtonPressed(sender: sender)
   }

   }

Here is my cellForRow at indexPath code:这是我的 cellForRow 在 indexPath 代码:

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "FinalCell", for: indexPath) as! FinalCell
        cell.delegate = self
        cell.cellTag = indexPath.row
        cell.addButton.tag = indexPath.row
        cell.subtractButton.tag = indexPath.row

        return cell
    }

And of course delegate implementation:当然还有委托实现:

extension FinalGameSettings: FinalCellDelegate {

    func addButtonPressed(sender: UIButton) {
        print("works")
    }
    func subtactButtonPressed(sender: UIButton) {
        print("This works too")
}
}

The problem is, that when I press on Button - nothing happens.问题是,当我按下按钮时 - 没有任何反应。 Please, give me a piece of advice to handle the problem!请给我一条建议来解决这个问题!

I copy your project and make my own.我复制你的项目并自己制作。 After debugging, i found this.调试后,我发现了这个。 The button in your view is not inside the view addAndSubtractView .您视图中的按钮不在视图addAndSubtractView内。 So that means you can't click it.所以这意味着你不能点击它。 And your addAndSubtractView does not show in the cell, It's hidden somewhere.而且您的addAndSubtractView没有显示在单元格中,它隐藏在某个地方。

po addButton.frame
▿ (60.0, -17.0, 30.0, 34.0)
  ▿ origin : (60.0, -17.0)
    - x : 60.0
    - y : -17.0
  ▿ size : (30.0, 34.0)
    - width : 30.0
    - height : 34.0

Try fixing your auto-layout.尝试修复您的自动布局。 I suggest you try SnapKit framework for better looking我建议您尝试使用 SnapKit 框架以获得更好的外观

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM