斯威夫特 5
首先创建protocol(delegate)。
protocol CustomDelegate: AnyObject{
func didSelectedTheButton(_ index: Int)
}
创建UITableViewCell 并将protocol 初始化为实例。并在您的 TableViewCell 中添加按钮和按钮操作。在按钮 Action 中使用协议(委托)。请找到以下代码以供参考
class TableViewCell: UITableViewCell {
lazy var button: UIButton! = {
let button = UIButton()
button.translatesAutoresizingMaskIntoConstraints = false
button.backgroundColor = .darkGray
button.setTitleColor(.white, for: .normal)
button.addTarget(self, action: #selector(didTaped(_:)), for: .touchUpInside)
button.tag = 1
button.setTitle("Tap", for: .normal)
return button
}()
weak var delegate: CustomDelegate?
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
self.contentView.backgroundColor = .clear
self.backgroundColor = .clear
self.contentView.addSubview(button)
self.button.heightAnchor.constraint(equalToConstant: 60).isActive = true
self.button.widthAnchor.constraint(equalToConstant: UIScreen.main.bounds.width * 0.66).isActive = true
self.button.centerXAnchor.constraint(equalTo: self.contentView.centerXAnchor).isActive = true
self.button.centerYAnchor.constraint(equalTo: self.contentView.centerYAnchor).isActive = true
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@objc func didTaped(_ sender: UIButton){
//Call the protocol function for receive the action in ViewController
delegate?.didSelectedTheButton(sender.tag)
}
}
接下来创建UIViewController 并将UITableView 集成到您的ViewController 中。在函数tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell 中,将您的视图控制器分配给在UITableViewCell 中初始化的委托
class SecondViewController: UIViewController {
lazy var tableView: UITableView! = {
let tableView = UITableView(frame: .zero, style: .grouped)
tableView.translatesAutoresizingMaskIntoConstraints = false
tableView.register(TableViewCell.self, forCellReuseIdentifier: String(describing: TableViewCell.self))
tableView.backgroundColor = .white
tableView.separatorColor = .clear
tableView.delegate = self
tableView.dataSource = self
return tableView
}()
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = .white
//
self.view.addSubview(self.tableView)
NSLayoutConstraint.activate([
self.tableView.topAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.topAnchor),
self.tableView.leadingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.leadingAnchor),
self.tableView.trailingAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.trailingAnchor),
self.tableView.bottomAnchor.constraint(equalTo: self.view.safeAreaLayoutGuide.bottomAnchor),
])
}
}
extension SecondViewController: UITableViewDelegate, UITableViewDataSource{
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: String(describing: TableViewCell.self), for: indexPath) as! TableViewCell
cell.button.tag = indexPath.row
//ViewController Assign for protocol delegate
cell.delegate = self
cell.button.setTitle("Button \(indexPath.row)", for: .normal)
return cell
}
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 100
}
}
extension SecondViewController: CustomDelegate{
//Received the button action for cell
/**
index - contains row value for button selected
*/
func didSelectedTheButton(_ index: Int) {
// Enter your code here what did you want when the tableViewCell button action
print(index)
}
}