简体   繁体   English

自定义表格视图单元格上的 UITapGestureRecognizer 不起作用

[英]UITapGestureRecognizer on custom table view cell doesn't work

I'm trying to add a gesture recognizer for an image in a custom table view cell.我正在尝试为自定义表格视图单元格中的图像添加手势识别器。 For some reason I can't seem to make it work though.出于某种原因,我似乎无法让它工作。 Here's my code这是我的代码

override func awakeFromNib() {
    super.awakeFromNib()

    heartImage.userInteractionEnabled = true
    let gesture = UITapGestureRecognizer(target: self, action:Selector("onHeartTap"))
    gesture.delegate = self
    heartImage.addGestureRecognizer(gesture)
}

func onHeartTap(sender: UITapGestureRecognizer) 
    print("TAPPED")
}

The function awakeFromNib() gets called correctly, but onHeartTap() never gets called when tapping the image.函数awakeFromNib() 被正确调用,但在点击图像时onHeartTap() 永远不会被调用。 What am I doing wrong?我究竟做错了什么?

User interaction was enabled for the image but not for the cell.为图像启用了用户交互,但未为单元格启用。

Enabled it for the cell from the storyboard and everything works.为故事板中的单元启用它,一切正常。 Thanks for the help everybody!谢谢大家的帮助!

In your selector name 'onHeartTap' ":" is missing在您的选择器名称“onHeartTap”中缺少“:”

just replace只需更换

let gesture = UITapGestureRecognizer(target: self, action:Selector("onHeartTap")) 

with

let gesture = UITapGestureRecognizer(target: self, action:Selector("onHeartTap:"))

And also any specific reason you are doing this in awakeFromNib??还有你在awakeFromNib中这样做的任何具体原因?? You can do this on viewWillAppear also.您也可以在 viewWillAppear 上执行此操作。

I had a similar problem, that a UIImageView in a UITableViewCell subclass would not trigger the gesture recognizer that I set up.我有一个类似的问题, UITableViewCell 子类中的 UIImageView 不会触发我设置的手势识别器。

func viewDidLoad() {
    imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(imageViewTapped(sender:))))
    imageView.isUserInteractionEnabled = true
}

the problem was the viewDidLoad() is not called for UITableViewCell:问题是没有为 UITableViewCell 调用viewDidLoad()
Can I use viewDidLoad method in UITableviewCell? 我可以在 UITableviewCell 中使用 viewDidLoad 方法吗?

set the gesture recognizer in awakeFromNib() instead:改为在awakeFromNib()设置手势识别器:

override func awakeFromNib() {
    imageView.addGestureRecognizer(UIGestureRecognizer(target: self, action: #selector(imageViewTapped(sender:))))
    imageView.isUserInteractionEnabled = true
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Scroll View中用于UIView的UITapGestureRecognizer不起作用 - UITapGestureRecognizer for UIView in Scroll View doesn't work 自定义 UIView 中的 UITapGestureRecognizer 不起作用 - UITapGestureRecognizer in custom UIView doesn't work IBAction在自定义视图单元上不起作用 - IBAction doesn't work on a custom view cell 带有UITapGestureRecognizer的UIView上的UITableView(单元格选择不起作用) - UITableView on a UIView with UITapGestureRecognizer (cell selection doesn't work) 无法将从NIB加载的视图添加到自定义表视图单元中 - Adding a view loaded from a NIB to a Custom Table View Cell doesn't work as expected 在自定义表格视图单元格中执行操作将不起作用 - Performing action in custom Table View Cell won't work 当UITapGestureRecognizer在prepareForSegue中点击并捕获其中的UIImage时,如何获取自定义表格视图单元格的索引路径 - How to get the indexpath of a custom table view cell when an UIImage inside of it is tapped and captured by UITapGestureRecognizer in prepareForSegue UITapGestureRecognizer在UICollectionView标头上不起作用 - UITapGestureRecognizer doesn't work on UICollectionView header 使用UITapGestureRecognizer在UIView之上的UIButton不起作用 - UIButton on top of UIView with UITapGestureRecognizer doesn't work Swift:以编程方式添加 UITapGestureRecognizer 不起作用 - Swift: programmatically add UITapGestureRecognizer doesn't work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM