点击未调用UITableView上的手势

时间:2014-05-07 04:35:56

标签: ios uitableview uigesturerecognizer

我正在尝试处理某些操作,当用户使用以下代码点击UITextField内的UItableView但未调用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];
}

2 个答案:

答案 0 :(得分:1)

你可以继承你自己的UITableViewCell并将触摸检测放在里面。

答案 1 :(得分:0)

我认为当你触摸那个时间UITableViewdidSelect时会调用委托方法。所以请遵循以下代码:

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

试试这个。我认为这可以帮助你:)