0

在我的 ViewController 中,我得到了-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath,但这永远不会被调用。我搜索了为什么没有调用它,我发现我使用了这个:

 UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                                   initWithTarget:self
                                   action:@selector(dismissKeyboard)];

    [self.view addGestureRecognizer:tap];

点击背景时关闭键盘。知道如何解决这个问题,dismisskeyboard 点击背景和didSelectRowAtIndexPath工作?

4

5 回答 5

0

如果您点击此视图。您必须覆盖您的委托方法shouldReceiveTouch才能返回NO。如果触摸发生在tableView

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch {

    // Disallow recognition of tap gestures in the segmented control.
    if ([touch.view isDescendantOfView:self.tblView]) {
        return NO;
    }
    return YES;
}

现在两个委托方法将触发.. Note:self.tblView 是一个 tableView

于 2013-04-08T09:25:39.563 回答
0
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer 
return TRUE;

好的,看看你如何处理那个......然后如果它有效,请阅读文档以获得全面的解释,以便您有一个清晰的理解。

于 2013-04-08T09:26:47.910 回答
0
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]
                               initWithTarget:self
                               action:@selector(dismissKeyboard:)];

[tbl addGestureRecognizer:tap];

-(void)dismissKeyboard:(UITapGestureRecognizer *)gestureRecognizer
{
    CGPoint p = [gestureRecognizer locationInView:tbl];

    NSIndexPath *indexPath = [tbl indexPathForRowAtPoint:p];
    if (indexPath == nil)
        NSLog(@"Resign");
    else
        NSLog(@"Did select Row, %i",indexPath.row);// Do your stuff as you are doing in didSelectRow
}

实现这个

于 2013-04-08T09:15:49.437 回答
0

didSelectRowAtIndexPath永远不会被调用,因为您可能没有实现UITableViewDelegate.

您应该将其与您的控件一起使用以从响应者中辞职

 [yourTextField resignFirstResponder];
于 2013-04-08T09:10:31.640 回答
0

像这样尝试它会帮助你,

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [[event allTouches] anyObject];
    if(![[touch view] isKindOfClass:[UITextField class]]){

//resign your textfield here
    }

}
于 2013-04-08T09:14:13.240 回答