Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/102.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 方法,在没有单元格时单击UITableView,以便键盘关闭_Iphone_Ios_Objective C - Fatal编程技术网

Iphone 方法,在没有单元格时单击UITableView,以便键盘关闭

Iphone 方法,在没有单元格时单击UITableView,以便键盘关闭,iphone,ios,objective-c,Iphone,Ios,Objective C,在屏幕上,用户可以单击文本字段加载选择器以选择位置。然后,我使用基于此位置的自定义单元格重新加载tableview中的所有元素。对于某些位置,可能没有要加载的内容,因此没有单元 当我有单元格,用户点击键盘时,这部分代码会被很好地点击: -(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath { [self.locationTextField resignFir

在屏幕上,用户可以单击文本字段加载选择器以选择位置。然后,我使用基于此位置的自定义单元格重新加载tableview中的所有元素。对于某些位置,可能没有要加载的内容,因此没有单元

当我有单元格,用户点击键盘时,这部分代码会被很好地点击:

-(void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [self.locationTextField resignFirstResponder];

    ...
}
我还有一段代码,可以很好地处理不点击任何东西的情况

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

但是当tableview没有单元格时,当用户单击tableview的空间时,这两个单元格都不会被激发。是否有其他设置可以检测该区域的触摸?

您可以尝试使用
UITapGestureRecognitizer
并将其添加到表视图中。 大概是这样的:

UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tableViewTap:)];
[self.myTableView addGestureRecognizer:tapRecognizer];
然后:

-(void) tableViewTap:(UIGestureRecognizer*)recognizer 
{
    CGPoint tapLocation = [recognizer locationInView:self.myTableView];
    NSIndexPath *indexPath = [self.myTableView indexPathForRowAtPoint:tapLocation];

    if (indexPath) //user tapped on a table cell
         recognizer.cancelsTouchesInView = NO;
    else //user tapped somewhere else on the table view
    {
        //your stuff here
    }
}

您可以将点击识别器添加到表视图或表视图的超级视图中。