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

Related searches

I am trying to attach a gesture recognizer on a button and on an image view in a dynamic tableView cell, but I get error:

*** 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() {
     private func initialize() {

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

      tapGestureRecognizerProfileName.addTarget(self.shareButton, action: #selector(MediaTableViewCell.nameOfuserTapped(gestureRecgonizer:)))

     func imageTapped(gestureRecgonizer: UITapGestureRecognizer) {
        delegate?.didClickProfileImageOf(cell: self)

   func nameOfuserTapped(gestureRecgonizer: UITapGestureRecognizer) {
     delegate?.didClickProfileNameOf(cell: self)

}//end class

Your question title is misleading -- You are adding gesture recognizer to the UIElements, not the whole cell.

Buttons don't need a gesture recognizer since they subclass from UIControl.

private func initialize() {
    let imageTapGesture = UITapGestureRecognizer(target: self, action: #selector(profileTapped))
    // 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)

-- Updated per comment --

protocol MediaTableViewCellDelegate: class {
    func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileImage: Bool)
    func mediaTableViewCell(_ cell: MediaTableViewCell, didClickProfileName: Bool)

How do I register a tap on UIImage view which is inside , How do I register a tap on UIImage view which is inside UITableView cell? to respond to tap, it wont call the function which is supposed to update the data. use a UIButton with an image instead of adding gesture recognizers to an image. Adding gesture recognizer to UIImage, not UIImageView. ios,objective-c,uiimageview,uiimage,uigesturerecognizer. As UIImage extends from NSObject you can not add UITapGestureRecognizer to it, you have to add gesture to the UIImageView itself.

You are mixing up the argument of addTarget with the caller of addGestureRecognizer.

The target of a gesture recognizer's addTarget is the class that implements the selector (the closure passed as action). Which in this case is self.

addGestureRecognizer adds the recognizer to the view itself.

So you want:

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


iOS, Adding tap gesture recognizer screenshot. Control drag from the gesture in the Document Outline to your View Controller code in order to make an Outlet and an � Choose a Gesture Recognizer from the Object Library based on the functionality your app needs. Step 2: Drag and Drop a Gesture Recognizer into your View. Drag a Gesture recognizer from the Object Library and drop it onto the View you want the Gesture to be attached to.

I think that you shouldn't add UIGestureRecognizer for a button, cuz it has already touch event.

Just add this code for button:

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

As for profileImageView add this lines of code:

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

iOS, To add a method to a button, first create an action method: Objective-C -(void) someButtonAction:(id)sender { // sender is the object that was tapped, in this case � Add the gesture recognizer to a view: You associate each gesture recognizer with one, and only one, view. When a touch occurs within the bounds of that view, the gesture recognizer will check if it matches the type of touch it’s looking for. If it finds a match, it notifies the target.

UITableViewCell using QuickDialog : handle touch event on all the , So how to change cell accessoryView position in UITableViewCell or set accessoryView with help of respected selector, you can change UIImage of UIButton. [self.button addGestureRecognizer:tapOnce]; //remove the other button action� Hi there, as the title suggests, I am unable to implement a swipe gesture recognizer on an outlet within a custom uitableviewcell. I have been able to add a tap gesture recognizer to the same outlet, so I'm confident that I'm doing it in the right place, but I was wondering whether the menu opening swipe on the detail page would be conflicting with the swipe that I'm trying to implement?

Introduction to Interface Builder, To add a gesture recognizer to an object in a .xib, just drag and drop the onto the storyboard, and add a UIImage, UILabel, UITextView, and two UIButtons to it, You want each of the UITableViewCells to segue to this App Details view when � void OnPointerPressed(object sender, PointerRoutedEventArgs args) { // Set the pointer capture to the element being interacted with so that only it // will fire pointer-related events element.CapturePointer(args.Pointer); // Feed the current point into the gesture recognizer as a down event recognizer.ProcessDownEvent(args.GetCurrentPoint

r/swift: Swift is a general-purpose programming language built using a modern approach to safety, performance, and software design patterns.