简体   繁体   English

TapGestureRecognizer在自定义TableViewCell中不起作用

[英]TapGestureRecognizer is not working in custom TableViewCell

I have strange issue - when I register TapGestureRecognizer in the cellForRowAtIndexPath method it works perfect, but when I register TapGestureRecognizer in cell's initWithStyle method tap recognition doesn't work, breakpoint doesn't hit in handler. 我有一个奇怪的问题-当我在cellForRowAtIndexPath方法中注册TapGestureRecognizer时,它可以正常工作,但是当我在单元格的initWithStyle方法中注册TapGestureRecognizer时,点击识别不起作用,则断点不会在处理程序中命中。

The following works. 以下作品。

I have created custom table view cell with corresponding xib file and registered it. 我已经使用相应的xib文件创建了自定义表格视图单元并进行了注册。

[self.tableView registerNib:[UINib nibWithNibName:@"MyCell"
                                               bundle:[NSBundle mainBundle]]
         forCellReuseIdentifier:@"cell"];
...

and in the cellForRowAtIndexPath 
MyCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
...
 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self
                                                                          action:@selector(didTapCell:)];
    [tap setNumberOfTapsRequired:1];
    [cell addGestureRecognizer:tap];

The following doesn't work 以下不起作用

@implementation MyCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        UITapGestureRecognizer *tgr = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleCellTap:)];
        [tgr setDelegate:self];
        [tgr setNumberOfTapsRequired:1];
        [tgr setNumberOfTouchesRequired:1];

        [self addGestureRecognizer:tgr];
        //[self.contentView addGestureRecognizer:tgr]; also doesn't work
    }
    return self;
}

I can leave the working solution, but I want to move the gesture recognition to cell initialization and fire tap event through my delegate. 我可以离开工作解决方案,但我想通过我的代表将手势识别移至单元格初始化和触发点击事件。

Why is tap recognition not working if I'm registering recognizer in the cell initialization? 如果我在单元格初始化中注册识别器,为什么点击识别不起作用?

Are you sure initWithStyle:reuseIdentifier is called? 您确定调用了initWithStyle:reuseIdentifier吗? Afaik you have to use initWithCoder: if you register a nib for the cell. Afaik您必须使用initWithCoder:如果您为单元格注册一个笔尖。

In a project of mine I have this 在我的一个项目中,我有这个

- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)];
        pan.delegate = self;
        self.gestureRecognizers = [NSArray arrayWithObject:pan];
    }
    return self;
}

So I am using a pan gesture recognizer and it works from within the init method. 因此,我使用的是平移手势识别器,它可以在init方法中使用。

You have registered a xib for a particular cell identifier. 您已经为特定的小区标识符注册了一个xib。 Now the tableview will automatically instantiate a cell for you if needed (when you call dequeReusableCell...) but the initWithStyle:reuseIdentifier method does not get called, so your gesture recognizer is never created/added. 现在,如果需要(当您调用dequeReusableCell ...时),表视图将自动为您实例化一个单元格,但不会调用initWithStyle:reuseIdentifier方法,因此永远不会创建/添加手势识别器。

If you do need to 'init' stuff when using registered xib(s), override the awakeFromNib in your custom cell class and put your code there. 如果在使用注册的xib时确实需要“初始化”内容,请在自定义单元格类中覆盖awakeFromNib并将代码放在此处。 I usually put my 'init' code in a separate method and call it from both initWithStyle and awakeFromNib overrides. 我通常将我的“ init”代码放在单独的方法中,并从initWithStyle和awakeFromNib重写中调用它。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM