UITableViewCell addTarget 中的按钮不起作用
Button in UITableViewCell addTarget does not work
在我的项目中,我有 table 带有 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")
}
}
首先去掉按钮变量惰性初始化中的add目标
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。
解决方法很简单。
在 init
函数中添加以下代码:
button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)
就是这样!
必须在惰性声明中或在初始化程序之后:
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
}()
这是因为目标视图必须先完成初始化,然后才能将选择器绑定到它。
好吧,也许我可以帮助解决目前对我有用的问题:
创建 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 class 中,正是在 init
方法中,而不是使用 :
addsubview(_actionButton)
使用
contentView.addSubview(_actionButton)
干杯
在我的项目中,我有 table 带有 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")
}
}
首先去掉按钮变量惰性初始化中的add目标
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。
解决方法很简单。
在 init
函数中添加以下代码:
button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)
就是这样!
必须在惰性声明中或在初始化程序之后:
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
}()
这是因为目标视图必须先完成初始化,然后才能将选择器绑定到它。
好吧,也许我可以帮助解决目前对我有用的问题:
创建 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 class 中,正是在 init
方法中,而不是使用 :
addsubview(_actionButton)
使用
contentView.addSubview(_actionButton)
干杯