我正在尝试在动态tableView单元格中的按钮和图像视图上附加手势识别器,但出现错误:
***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[UIButton nameOfuserTappedWithGestureRecgonizer:]:无法识别的选择器发送到 实例0x7ff34253f5a0'
protocol MediaTableViewCellDelegate: class {
func didClickProfileImageOf(cell: MediaTableViewCell)
func didClickProfileNameOf(cell: MediaTableViewCell)
}
class MediaTableViewCell: UITableViewCell {
weak var delegate: MediaTableViewCellDelegate?
@IBOutlet weak var mediaImageView: UIImageView! //the large image
@IBOutlet weak var profileImageView: UIImageView!
@IBOutlet weak var fullNameButton: UIButton!
var tapGestureRecognizerProfileImage = UITapGestureRecognizer()
var tapGestureRecognizerProfileName = UITapGestureRecognizer()
override func awakeFromNib() {
super.awakeFromNib()
initialize()
}
private func initialize() {
tapGestureRecognizerProfileImage.addTarget(self.profileImageView, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))
self.addGestureRecognizer(tapGestureRecognizerProfileImage)
tapGestureRecognizerProfileName.addTarget(self.shareButton, action: #selector(MediaTableViewCell.nameOfuserTapped(gestureRecgonizer:)))
self.addGestureRecognizer(tapGestureRecognizerProfileName)
}
func imageTapped(gestureRecgonizer: UITapGestureRecognizer) {
delegate?.didClickProfileImageOf(cell: self)
}
func nameOfuserTapped(gestureRecgonizer: UITapGestureRecognizer) {
delegate?.didClickProfileNameOf(cell: self)
}
}//end class
答案 0 :(得分:2)
您的问题标题具有误导性-您正在向UIElement添加手势识别器,而不是整个单元格。
按钮不需要手势识别器,因为它们是UIControl的子类。
private func initialize() {
let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(profileTapped))
profileImageView.addGestureRecognizer(imageTapGesture)
// imageviews by default aren't interactable
profileImageView.isUserInteractionEnabled = true
fullNameButton.addTarget(self, action: #selector(buttonTapped), for: .touchUpInside)
}
@objc private func profileTapped() {
delegate?.didClickProfileImageOf(cell: self)
}
@objc private func buttonTapped() {
delegate?.didClickProfileNameOf(cell: self)
}
-每个评论已更新-
protocol MediaTableViewCellDelegate: class {
func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileImage: Bool)
func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileName: Bool)
}
答案 1 :(得分:0)
您正在将addTarget
的参数与addGestureRecognizer
的调用者混合在一起。
手势识别器的addTarget
的目标是实现选择器的类(以action
传递的闭包)。在这种情况下为self
。
addGestureRecognizer
将识别器添加到视图本身。
所以你想要
tapGestureRecognizerProfileImage.addTarget(self, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))
self.profileImageView.addGestureRecognizer(tapGestureRecognizerProfileImage)
答案 2 :(得分:0)
我认为您不应该为按钮添加UIGestureRecognizer,因为它已经具有触摸事件。
只需为按钮添加以下代码:
series = df["FIRST_NAME"]+","+df["FULL_NAME"]
series = series.apply(lambda x: x.split(','))
series = series.apply(lambda x: True if x[0] in x[1] else False)
对于profileImageView,添加以下代码行:
fullNameButton.addTarget(self, action: #selector(some selector), for: .touchUpInside)