私は奇妙な問題を抱えています - 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;
}
作業ソリューションを残すことができますが、ジェスチャ認識をセルの初期化に移動し、デリゲートを介してタップ イベントを発生させたいと考えています。
セルの初期化で認識エンジンを登録している場合、タップ認識が機能しないのはなぜですか?