当 UISearchDisplayController 处于非活动状态时保持搜索词可见
我的应用程序使用 UISearchDisplayController
。当用户输入搜索词时,我希望它在搜索栏中保持可见。如果用户选择匹配的结果之一,则此方法有效,但如果用户单击“搜索”按钮,则此方法无效。
这有效:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSString *selectedMatch = [self.searchMatches objectAtIndex:indexPath.row];
[self.searchDisplayController setActive:NO animated:YES];
[self.searchDisplayController.searchBar setText:selectedMatch];
return;
}
...
但是如果我在 -searchBarSearchButtonClicked:
中执行相同的操作,文本不会保留在搜索栏中。关于在这种情况下我如何实现这一目标有什么想法吗?
相关的是,如果我设置搜索栏的文本(但使 UISearchDisplayController 保持不活动状态),则会触发 searchResultsTableView 的显示。我只想在用户点击搜索栏时显示这一点。
编辑:找到了一种解决方法来设置搜索栏的文本而不随时显示searchResultsTableView:
// This hacky YES NO is to keep results table view hidden (animation required) when setting search bar text
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = @"text to show";
仍然欢迎更好的建议!
My app uses an UISearchDisplayController
. When the user has entered a search term I want it to remain visible in the searchbar. This works if the user chooses one of the matched results, but not if the user clicks the 'Search' button.
This works:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView == self.searchDisplayController.searchResultsTableView) {
NSString *selectedMatch = [self.searchMatches objectAtIndex:indexPath.row];
[self.searchDisplayController setActive:NO animated:YES];
[self.searchDisplayController.searchBar setText:selectedMatch];
return;
}
...
But if I do the same thing in -searchBarSearchButtonClicked:
the text doesn't remain in the searchbar. Any ideas on how I can accomplish this in this situation?
Related, if I set the text of the searchbar (but leave the UISearchDisplayController
inactive) this triggers the display of the searchResultsTableView. I only want to show that when the user taps on the searchbar.
Edit: Found a workaround to set the text of a searchbar without showing the searchResultsTableView at any time:
// This hacky YES NO is to keep results table view hidden (animation required) when setting search bar text
[self.searchDisplayController setActive:YES animated:YES];
[self.searchDisplayController setActive:NO animated:YES];
self.searchDisplayController.searchBar.text = @"text to show";
Better suggestions still welcomed!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您不能在 searchBarSearchButtonClicked 方法中使用相同的代码,因为您没有 indexPath 来选择 searchMatches 数组中的正确元素。
如果用户单击搜索按钮并且您想要隐藏 searchController 界面,您必须弄清楚要在搜索中放入哪些文本(例如,在列表中选择最佳匹配结果)。
此示例只是让搜索词在用户单击搜索按钮时保持可见且不变:
希望这有帮助,
文森特
Actually you cannot use the same code inside the searchBarSearchButtonClicked method because you have no indexPath to select the right element in your searchMatches array.
If the user clicks the search button and you want to hide the searchController interface you have to figure out what text to put inside the search (for example selecting the best matching result in the list).
This example just let the search term remain visible and unchanged when the user clicks the search button :
Hope this helps,
Vincent
手动重置搜索栏中的字符串会再次触发一些 UISearchDisplayDelegate 方法。在这种情况下,这可能是您不想要的。
我会稍微修改一下 vdaubry 答案,它给了我:
Resetting manually the string in the search bar triggers some of the UISearchDisplayDelegate methods again. This is something you probably don't want, in this case.
I would modify vdaubry answer a bit, and it gives me: