【问题标题】:Button in UITableViewCell addTarget does not workUITableViewCell addTarget 中的按钮不起作用
【发布时间】:2018-08-27 07:31:24
【问题描述】:

在我的项目中,我有带有 3 个按钮的单元格的表格视图。当我为他们添加目标操作时,它不起作用。

为了避免与其他项目的不良交互,我创建了一个干净的项目并尝试通过addTarget 来按钮TableViewCell。即使在这里,它也不起作用。 在这个项目中,我为每一行设置了一个按钮。

addTarget 方法应该调用presentWebBrowser 方法。但没有。 有什么想法吗?

这是我的代码:

import UIKit

class TableViewController: UITableViewController {

let array = ["button1","button2","button3"]

override func viewDidLoad() {
    super.viewDidLoad()
tableView.register(MyCell.self, forCellReuseIdentifier: "cellID")

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

// MARK: - Table view data source



override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
    // #warning Incomplete implementation, return the number of rows
    return array.count
}


override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {

    let cell = tableView.dequeueReusableCell(withIdentifier: "cellID", for: indexPath) as! MyCell

    return cell

}

}

class MyCell: UITableViewCell {



var button: UIButton = {
    let button = UIButton(type: UIButtonType.system)
    button.setTitle("Button", for: UIControlState.normal)
     button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)

    // button.titleLabel?.font = UIFont.boldSystemFont(ofSize: 8)

    return button
}()


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

    addSubview(button)
    button.translatesAutoresizingMaskIntoConstraints = false
    button.topAnchor.constraint(equalTo: topAnchor).isActive = true
    button.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
    button.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true



}


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

@objc func presentWebBrowser(sender: UIButton!) {
    print("tapped")

}



}

【问题讨论】:

  • 我认为上面的代码甚至不会运行,因为当使用dequeueReusableCell init?(coder 将单元格出列并且你有一个fatalError 因此应用程序将崩溃
  • Interface Builder 有什么问题(IBActionIBOutlet)?
  • 我不使用情节提要。

标签: ios swift uitableview cocoa-touch


【解决方案1】:

好吧,也许我可以帮助解决目前为止对我有用的方法:

创建 UIButton

lazy var _actionButton: UIButton = {
        let button = UIButton()
        button.setImage(UIImage(named: "reset"), for: .normal)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.addTarget(self, action: #selector(handleAction), for: .touchUpInside)
        return button
    }()

@objc 函数

@objc func handleAction(){
    print("test reset via action button...")
}

在tableviewcell类中,正好在init方法中,而不是使用:

addsubview(_actionButton)

使用

contentView.addSubview(_actionButton)

干杯

【讨论】:

    【解决方案2】:

    必须在惰性声明中或在初始化器之后:

    lazy var button: UIButton = {
       let button = UIButton(type: .system)
       button.setTitle("Button", for: UIControlState.normal)
       button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)
       return button
    }()
    

    这是因为目标视图必须在选择器绑定到它之前完成初始化。

    【讨论】:

      【解决方案3】:

      解决方法很简单。 在init函数中添加以下代码:

      button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)
      

      就是这样!

      【讨论】:

        【解决方案4】:

        先去掉按钮变量延迟初始化中的add target

        var button: UIButton = {
            let button = UIButton(type: UIButtonType.system)
            button.setTitle("Button", for: UIControlState.normal)
            return button
        }()
        

        在init中修改你的代码为

        required init?(coder aDecoder: NSCoder) {
            super.init(coder: aDecoder)
            addSubview(button)
            button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)
            button.translatesAutoresizingMaskIntoConstraints = false
            button.topAnchor.constraint(equalTo: topAnchor).isActive = true
            button.leftAnchor.constraint(equalTo: leftAnchor).isActive = true
            button.bottomAnchor.constraint(equalTo: bottomAnchor).isActive = true
        
        }
        

        注意在延迟初始化之外在 button 上使用 addTarget。

        【讨论】:

        • @angelus : 很高兴我能帮上忙 :) 编码愉快 :)
        猜你喜欢
        • 2018-01-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-24
        相关资源
        最近更新 更多