[英]Button in UITableViewCell addTarget does not work
在我的項目中,我有帶有 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 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。
必須在惰性聲明中或在初始化程序之后:
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
}()
這是因為目標視圖必須在選擇器綁定到它之前完成初始化。
解決方法很簡單。 在init
函數中添加以下代碼:
button.addTarget(self, action: #selector(presentWebBrowser), for: .touchUpInside)
而已!
好吧,也許我可以幫助解決目前對我有用的問題:
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 func handleAction(){
print("test reset via action button...")
}
在 tableviewcell 類中,正是在init
方法中,而不是使用:
addsubview(_actionButton)
采用
contentView.addSubview(_actionButton)
干杯
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.