如何在自定义的TableviewCell中按下UIButton



在整个SO中,我一直在研究如何在自定义的表视图单元格中与UIButton交互。我看到的所有答案都是使用IBOutlets,但我还没有看到完全通过编程实现这一点的方法。我习惯于通过button.addTarget与按钮交互。这是我的两个ViewController,一个是自定义的tableviewcell,另一个是ViewController。

这是我定制的。我尝试使用协议委派路由,但是失败了。

import UIKit

@objc protocol TableViewNew {
func onClickCell()
}

class NewMoveTableViewCell: UITableViewCell {

var cellDelegate: TableViewNew?




static let identifier = "NewTableViewCell"

private let myImageView: UIImageView = {
let imageView = UIImageView()
imageView.clipsToBounds = true
imageView.contentMode = .scaleAspectFill
imageView.layer.masksToBounds = true
imageView.backgroundColor = .purple
imageView.layer.cornerRadius = 80/2
return imageView
}()

private let myLabel : UILabel = {
let label = UILabel()
label.text = "test"
label.backgroundColor = .blue
label.textColor = .systemPink
label.adjustsFontSizeToFitWidth = true
label.textAlignment = .center

return label
}()

private let button: UIButton = {
let button = UIButton()
button.setTitle("Invite", for: .normal)
button.backgroundColor = .systemPink
button.layer.cornerRadius = 10
return button
}()
override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
super.init(style: style, reuseIdentifier: reuseIdentifier)
addSubview(myImageView)
addSubview(myLabel)
addSubview(button)
setImageConstratins()
setTitleLabelConstraints()
setButton()


}

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

func setImageConstratins() {

myImageView.translatesAutoresizingMaskIntoConstraints = false
myImageView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
myImageView.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 12).isActive = true
myImageView.heightAnchor.constraint(equalToConstant: 80).isActive = true
myImageView.widthAnchor.constraint(equalToConstant: 80).isActive = true

}

func setTitleLabelConstraints() {
myLabel.translatesAutoresizingMaskIntoConstraints = false
myLabel.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
myLabel.leadingAnchor.constraint(equalTo: myImageView.trailingAnchor, constant: 5).isActive = true
myLabel.heightAnchor.constraint(equalToConstant: 80).isActive = true
//myLabel.trailingAnchor.constraint(equalTo: button.leadingAnchor, constant: -12).isActive = true

}

func setButton() {
button.translatesAutoresizingMaskIntoConstraints = false
button.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
button.leadingAnchor.constraint(equalTo: myLabel.trailingAnchor, constant: -5).isActive = true
button.heightAnchor.constraint(equalToConstant: 80).isActive = true
button.widthAnchor.constraint(equalToConstant: 150).isActive = true
button.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -12).isActive = true
}

public func configure(with name: String, label: String) {
myLabel.text = label
myImageView.image = UIImage(named: name)
}



override func prepareForReuse() {
super.prepareForReuse()
myLabel.text = nil
myImageView.image = nil
}


@objc func didTapButton(_ sender: Any) {
cellDelegate?.onClickCell()
}



}

其次,这里是TableView所在的ViewController。


import UIKit
class NewMoveViewController: UIViewController {

private let tableView: UITableView = {
let tableView = UITableView()
tableView.rowHeight = 100




return tableView
}()



private var collectionView: UICollectionView?



override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(tableView)

let layout = UICollectionViewFlowLayout()
layout.scrollDirection = .horizontal
layout.itemSize = CGSize(width: 50, height: 50)
collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)

collectionView?.register(NewMoveCollectionViewCell.self, forCellWithReuseIdentifier: NewMoveCollectionViewCell.identifier)
collectionView?.showsHorizontalScrollIndicator = false 

title = "Add to Group"
tableView.register(NewMoveTableViewCell.self, forCellReuseIdentifier: NewMoveTableViewCell.identifier)
tableView.delegate = self
tableView.dataSource = self

collectionView?.backgroundColor = .systemBlue
collectionView?.dataSource = self
collectionView?.delegate = self

guard let myCollection = collectionView else {
return
}

view.addSubview(myCollection)
// Do any additional setup after loading the view.
}

override func viewDidLayoutSubviews() {
super.viewDidLayoutSubviews()
collectionView?.frame = CGRect(x: 0, y: 100, width: view.frame.size.width, height: 50)
tableView.frame = CGRect(x: 0, y: 200, width: view.frame.size.width, height: view.frame.size.height)

}


}
extension NewMoveViewController : UITableViewDelegate, UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return 10
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: NewMoveTableViewCell.identifier, for: indexPath) as! NewMoveTableViewCell
cell.cellDelegate = self
cell.configure(with: "", label: "test")


return cell
}

func tableView(_ tableView: UITableView, shouldHighlightRowAt indexPath: IndexPath) -> Bool {
return false
}




}

extension NewMoveViewController : UICollectionViewDelegate, UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return 1
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: NewMoveCollectionViewCell.identifier, for: indexPath) as! NewMoveCollectionViewCell


return cell
}




}

extension NewMoveViewController : TableViewNew {
func onClickCell() {
print("Pressed")
}

我使这个ViewController符合自定义单元格的协议,并将该函数放入单元格的cellForRowAt函数中。当我使用这条路线时,我会运行我的应用程序,一切都会很好,但当我试图点击视图控制器中的自定义表视图单元格时,什么也没发生。如有任何帮助,我们将不胜感激。

您需要将操作处理程序添加到您的按钮:

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

最新更新