UITableView上的UITapGesture没有被调用



我试图处理一些操作,当用户点击UItableView内的UITextField与以下代码,但tapDetected:代码没有被调用。

UITableViewCell *cell = [[UITableViewCell alloc] init];
    UITextField *txtComment=[[UITextField alloc]initWithFrame:CGRectMake(10, 10,cell.contentView.frame.size.width-30 , 30)];
    [txtComment setText:NSLocalizedString(@"Comment Here", nil)];
    [txtComment setFont:mediumFont];
    [txtComment setTintColor:[UIColor grayColor]];
    [txtComment setTag:newCommentTag];
    txtComment.layer.masksToBounds=YES;
    txtComment.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    txtComment.layer.borderWidth= 0.3f;
    UITapGestureRecognizer *tap =
            [[UITapGestureRecognizer alloc]
                       initWithTarget:self
                       action:@selector(tapDetected:)];
    tap.numberOfTapsRequired = 1;
    [txtComment addGestureRecognizer:tap];
    [cell.contentView addSubview:txtComment];

- (IBAction)tapDetected:(UIGestureRecognizer *)sender
{
[[NSNotificationCenter defaultCenter] postNotificationName:spAddNewCommentNotification object:nil];
}

你可以子类化你自己的UITableViewCell并把触摸检测放到里面

我想当你触摸UITableViewdidSelect委托方法被调用的时候。所以请遵循以下代码:

 - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch
 {
    if ([touch.view isDescendantOfView:cell])
    {
       return NO;
    }   
    return YES;
 }

试试这个。我想这对你有帮助

最新更新