Why won't the selector method of the gesture recognizer execute?

I am not able to get the selector of a tap gesture recognizer to execute. Here is my code of the table view cell that the gesture recognizer exists in:

import UIKit

class DueDateTableViewCell: UITableViewCell {

    var label = UILabel()
    var tapGestureRecognizer: UITapGestureRecognizer!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        
        label.translatesAutoresizingMaskIntoConstraints = false
        contentView.addSubview(label)
        
        NSLayoutConstraint(item: label, attribute: .leading, relatedBy: .equal, toItem: contentView, attribute: .leadingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: label, attribute: .trailing, relatedBy: .equal, toItem: contentView, attribute: .trailingMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: label, attribute: .top, relatedBy: .equal, toItem: contentView, attribute: .topMargin, multiplier: 1, constant: 0).isActive = true
        NSLayoutConstraint(item: label, attribute: .bottom, relatedBy: .equal, toItem: contentView, attribute: .bottomMargin, multiplier: 1, constant: 0).isActive = true
        
        tapGestureRecognizer = UITapGestureRecognizer(target: DetailTableViewController(), action: #selector(DetailTableViewController.actionShowDatePicker))
//        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(self.actionShowDatePicker))
        self.addGestureRecognizer(tapGestureRecognizer)
        
    }

    @objc func actionShowDatePicker() {
        
        print("!!!")
        
    }
    
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

Here is the selector function in the DetailTableViewController class that the tap gesture recognizer calls from the table view cell after I’ve cut out all the code under the print statement in the function:

@objc func actionShowDatePicker() {
    
    print("actionShowDatePicker()")
    
    datePickerVisible = !datePickerVisible
    
    if datePickerVisible {
        
        let indexPath = IndexPath(row: 2, section: 0)
        
        tableView.insertRows(at: [indexPath], with: .top)
        
    } else {
        
        
        let indexPath = IndexPath(row: 2, section: 0)
        
        tableView.deleteRows(at: [indexPath], with: .top)
        
    }
    
}

The print statement never executes. I’m able to get it to work when I put the selector function in the table view cell class and adjust the line of code where I configure the gesture recognizer accordingly.

I am not getting any errors, and I am sure the code in tableView(_:cellForRowAt:) is executing properly because text that is assigned to the label property in the table view cell is showing as it should.

This topic was automatically closed after 166 days. New replies are no longer allowed.