我有一个奇怪的问题 - 当我在 cellForRowAtIndexPath 方法中注册 TapGestureRecognizer 时,它工作得很好,但是当我在单元格的 initWithStyle 方法中注册 TapGestureRecognizer 时,点击识别不起作用,断点不会在处理程序中命中。
以下作品。
我已经使用相应的 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];
以下不起作用
@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;
}
我可以离开工作解决方案,但我想通过我的委托将手势识别移动到单元初始化和触发点击事件。
如果我在单元初始化中注册识别器,为什么点击识别不起作用?