gpt4 book ai didi

ios - 在 UITableView 中搜索后点击坐标错误

转载 作者:行者123 更新时间:2023-11-29 10:51:18 24 4
gpt4 key购买 nike

我很确定我的问题的答案很简单,但经过几个小时的反复思考后,我转向你...

我有一个带有 UISearchBar 的 UITableView。搜索似乎工作得很好,但是当我搜索并点击一个单元格时,我得到的是可见单元格的 IndexPath,而不是单元格相对于整个 UITableView 的坐标。

现在,我很确定我的问题在于/围绕我使用手势而不是 didSelectRowAtIndex 的事实,因为我需要处理单击和双击。我已将手势识别器附加到 View 中。

单击/双击识别器通过以下方式获取选定的表格单元格:

CGPoint location = [gesture locationInView:self.tableView];
NSIndexPath *cellIndexPath = [self.tableView indexPathForRowAtPoint:location];

我的 UITableView 的大小可以在需要滚动之前显示大约 8 行。如果没有在 UISearchBar 中输入文本,这可以正常工作(两种手势)。如果我滚动到 UITableView 的第 15 行并点击,cellIndexPath 会返回第 15 行。

但是,如果我输入一个需要用户滚动的搜索词,点击第一个屏幕后的任何单元格会返回该行相对于其当前可见位置的 cellIndexPath(这意味着如果我滚动到第 10 行并将它定位到 UITableView 的最后一行,点击给我一个 8 的行索引。

所以,基本上,我试图弄清楚为什么不搜索 UITableView 时与输入搜索词时行为不同。

感谢您的帮助,罗布

最佳答案

哇,有时写下问题会让您找到解决方案……这就是我的体会。我将 UISearchBar 与 UISearchDisplayController 一起使用。我的初步理解是,在搜索时,UISearchDisplayController 会用自己的 tableView 替换 tableView 来显示结果。在这种情况下,我上面的代码在搜索时试图获取被点击的单元格时查看了错误的 UITableView。所以,我修改了如下代码,一切似乎都很好。

CGPoint location;
NSIndexPath *cellIndexPath;
if (self.isSearching)
{
location = [gesture locationInView:self.searchDisplayController.searchResultsTableView];
cellIndexPath = [self.searchDisplayController.searchResultsTableView indexPathForRowAtPoint:location];
}
else
{
location = [gesture locationInView:self.tableView];
cellIndexPath = [self.tableView indexPathForRowAtPoint:location];
}

关于ios - 在 UITableView 中搜索后点击坐标错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20352992/

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