gpt4 book ai didi

ios - 检测点击 UITableView 的空白区域

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:11:33 25 4
gpt4 key购买 nike

我有一个 UIViewController,它下面有一个 UITextFiled 和一个 UITableView。我在 UITableView 上使用 UITextField 作为搜索框。

当我在 UITextField 中点击时,会出现虚拟键盘,当我点击 UITableView 的任何区域时,我希望它消失。当用户点击 UITableView 和 UITextField 以外的任何区域时,键盘也应该消失。如果键盘不可见并且用户点击任何 UITableViewCell,那么我需要打开另一个 UIViewController。

这是它的样子。

enter image description here

当前实现:我正在使用 UITapGestureRecognizer 来检测点击位置。此外,我正在使用 shouldReceiveTouch 来查找触摸是否在 UITableView 上。如果是,那么我返回 NO,这意味着我忽略了 UITableView 上的触摸。如果否,那么我将返回 YES,并且在 UITapGestureRecognizer 处理程序中我将通过调用关闭键盘

    [self.searchTextField resignFirstResponder];

现在我面临的问题是: 当我的 UITableView 没有足够的数据并且它只显示 1 或 2 个单元格时,在这种情况下,如果我点击UITableView 的空白区域,但这不会,因为点击是在 UITableView 上,shouldReceiveTouch 将返回 NO 并忽略触摸。

注意 我首先实现 shouldReceiveTouch 的原因是我希望在用户点击任何单元格时自动调用 didSelectRowAtIndexPath 函数。只有当我忽略 UITableView 上的 Tapgesture 时,这才有可能。

我尝试了另一种方法,其中我没有实现 shouldReceiveTouch 并且在每次点击时我都调用 UITapGestureRecognizer 处理程序并检测触摸是否在 UITableView 内。在那种情况下,我没有得到被点击的正确行索引。我是这样实现的:

-(void)dismissKeyboard:(UITapGestureRecognizer *)tap
{
if([self.searchTextField isFirstResponder])
{
[self.searchTextField resignFirstResponder];
return;
}

CGPoint location = [tap locationInView:self.view];

if(touchedOutsideUITableView)
return;

NSIndexPath *path = [self.lotsTableView indexPathForRowAtPoint:location];

if(path)
{
// user taps on existing row
[self tableView:self.lotsTableView didSelectRowAtIndexPath:path];
}
else
{
// handle tap on empty area of UITableView
}
}

有人能告诉我如何在我当前的实现中检测 UITableView 空白区域上的点击吗?

最佳答案

如果只保留 UITapGestureRecognizer 但将 cancelsTouchesInView 设置为 NO 呢?默认为是

这样,当您点击任何单元格时,您的 didSelectRowAtIndexPath 方法仍会被调用。

关于ios - 检测点击 UITableView 的空白区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33528580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com