我有一個奇怪的問題 - 當我在cellForRowAtIndexPath方法中註冊TapGestureRecognizer時,它的工作原理非常完美,但是當我在單元的initWithStyle方法中註冊TapGestureRecognizer時,點擊識別不起作用,斷點不會在處理程序中命中。TapGestureRecognizer無法在自定義TableViewCell中工作
以下的作品。
我已經創建了具有相應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;
}
我可以離開工作的解決方案,但我想通過我的代表對移動手勢識別細胞的初始化和消防水龍頭事件。
爲什麼如果我在單元初始化中註冊識別器,輕拍識別不起作用?
更改此行[tgr setNumberOfTouchesRequired:1];與[tgr setNumberOfTouchesRequired:2] ;,它爲我工作... – 2013-02-12 11:07:27