如何将手势识别器添加到 UITableViewCell 的 UIImage 和 UIButton?

how to add gesture recognizer to UIImage and UIButton of UITableViewCell?

我试图在动态 tableView 单元格中的按钮和图像视图上附加手势识别器,但出现错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton nameOfuserTappedWithGestureRecgonizer:]: unrecognized selector sent to instance 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

您混淆了 addTarget 的参数和 addGestureRecognizer 的调用者。

手势识别器 addTarget 的目标是实现选择器的 class(作为 action 传递的闭包)。在这种情况下是 self.

addGestureRecognizer 将识别器添加到视图本身。

所以你想要:

tapGestureRecognizerProfileImage.addTarget(self, action: #selector(MediaTableViewCell.imageTapped(gestureRecgonizer:)))

self.profileImageView.addGestureRecognizer(tapGestureRecognizerProfileImage)

您的问题标题具有误导性 -- 您正在将手势识别器添加到 UIElements,而不是整个单元格。

按钮不需要手势识别器,因为它们是 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)
}

我认为您不应该为按钮添加 UIGestureRecognizer,因为它已经有触摸事件。

只需为按钮添加此代码:

fullNameButton.addTarget(self, action: #selector(some selector), for: .touchUpInside)

至于 profileImageView 添加这行代码:

let tapRecognizer = UITapGestureRecognizer(target: self, action: #selector(some selector)) 
profileImageView.isUserInteractionUnabled = true
profileImageView.addGestureRecognizer(tapRecognizer)