searchdisplaycontroller:更改搜索栏的文本
问题简短描述: 我想设置搜索栏的文本,而不自动触发与其绑定的搜索显示控制器。
问题的详细描述: 我有一个带有搜索栏和搜索显示控制器的 iPhone 应用程序。 searchdisplaycontroller 用于自动完成。对于自动完成,我使用 sqlite 数据库。用户输入关键字的前几个字母,结果显示在搜索显示控制器的表格中。对每个键入的字符执行 sql select 查询。这部分工作正常,字母已输入,结果可见。
问题如下:如果用户从表中选择一行,我想将搜索栏的文本更改为在自动完成结果表中选择的文本。我还想隐藏搜索显示控制器。这是行不通的。搜索显示控制器消失后,搜索栏中的文本框为空。我不知道出了什么问题。我不认为像更改文本框的文本这样简单的事情会变得如此复杂。
我尝试用两种方法更改文本: 首先在 didSelectRowAtIndexPath 方法中(对于搜索显示控制器的搜索结果表),但这没有帮助。当搜索显示控制器处于活动状态(动画消失)时,文本就在那里,但之后文本框为空。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRowAtIndexPath");
if (allKeywords.count > 0)
{
didSelectKeyword = TRUE;
self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row];
NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword);
shouldReloadResults = FALSE;
[[self.mainViewController keywordSearchBar] setText: selectedKeyword];
//shouldReloadResults = TRUE;
[[mainViewController searchDisplayController] setActive:NO animated: YES];
}
}
我还尝试在 searchDisplayControllerDidEndSearch 方法中更改它,但这也没有帮助。文本框……又……空了。
编辑:实际上它不是空的,文本在那里,但在消失的动画之后,自动完成表的结果仍然在那里。所以最终情况会变得更糟。
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
NSLog(@"searchDisplayControllerDidEndSearch");
if (didSelectKeyword)
{
shouldReloadResults = FALSE;
NSLog(@"keyword sdc didendsearch: %@", selectedKeyword);
[[mainViewController keywordSearchBar] setText: selectedKeyword]; //
在这个方法中,我调用另一个从 sqlite 中选择数据的方法。这部分正在发挥作用。
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
NSLog(@"shouldReloadResults : %i", shouldReloadResults);
if (shouldReloadResults)
{
NSLog(@"shouldReloadTableForSearchString: %@", searchString);
[self GetKeywords: searchString : @"GB"];
NSLog(@"shouldReloadTableForSearchString vege");
return YES;
}
return NO;
}
如果不清楚我的问题是什么,请询问我。我需要你的帮助。谢谢
SHORT DESCRIPTION OF PROBLEM:
I want to set the text of a searchbar without automatically triggering the search display controller that is bound to it.
LONG DESCRIPTION OF PROBLEM:
I have an iphone application with a search bar and a search display controller. The searchdisplaycontroller is used for autocomplete. For autocomplete i use an sqlite database. The user enters the first few letters of a keyword and the result are shown in the table of the searchdisplaycontroller. An sql select query is executed for every character typed. this part works ok, the letters have been entered and the results are visible.
The problem is the following: If the user selects a row from the table I want to change the text of the searchbar to the text that was selected in the autocomplete results table. I also want to hide the search display controller. This is not working. After the search display controller disappears the textbox in the search bar is empty. I have no idea what's wrong. I didn't think something so simple as changing the text of a textbox can get so complicated.
I have tried to change the text in 2 methods:
First in the didSelectRowAtIndexPath method (for the search results table of the search display controller), but that didn't help. The text was there while the search display controller was active (animating away) but after that the textbox was empty.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(@"didSelectRowAtIndexPath");
if (allKeywords.count > 0)
{
didSelectKeyword = TRUE;
self.selectedKeyword = (NSString *)[allKeywords objectAtIndex: indexPath.row];
NSLog(@"keyword didselectrow at idxp: %@", self.selectedKeyword);
shouldReloadResults = FALSE;
[[self.mainViewController keywordSearchBar] setText: selectedKeyword];
//shouldReloadResults = TRUE;
[[mainViewController searchDisplayController] setActive:NO animated: YES];
}
}
I also tried to change it in the searchDisplayControllerDidEndSearch method but that didn't help either. The textbox was...again.. empty.
edit: actually it wasnt empty the text was there but after the disappearing animation the results of the autocomplete table are still there. So it ends up getting worse.
- (void)searchDisplayControllerDidEndSearch:(UISearchDisplayController *)controller
{
NSLog(@"searchDisplayControllerDidEndSearch");
if (didSelectKeyword)
{
shouldReloadResults = FALSE;
NSLog(@"keyword sdc didendsearch: %@", selectedKeyword);
[[mainViewController keywordSearchBar] setText: selectedKeyword]; //
In this method i call another method which selects the data from sqlite. This part is working.
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString { NSLog(@"shouldReloadResults : %i", shouldReloadResults); if (shouldReloadResults) { NSLog(@"shouldReloadTableForSearchString: %@", searchString); [self GetKeywords: searchString : @"GB"]; NSLog(@"shouldReloadTableForSearchString vege"); return YES; } return NO; }
Please ask me if it's not clear what my problem is. I need your help. Thank you
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你没有尝试过吗:
mainViewController.searchDisplayController.searchBar.text=selectedKeyword;
您可以从搜索控制器访问搜索栏字段并更新其文本内容。
Didn't you try :
mainViewController.searchDisplayController.searchBar.text=selectedKeyword;
You access the search bar field from the search controller and you update its text content.
现在它起作用了我不知道发生了什么。我想我在更改文本后尝试停用搜索显示控制器。不管怎样,谢谢你的帮助。我想我还尝试更改原始文本字段,而不是属于搜索显示控制器的文本字段,并且它们是不同的(据我观察,内存中的地址不同)
Now it works i don't know what happened. I think I tried to deactivate the search display controller after changing the text. Anyway thanks for help. I think I also tried to change the original textfield not the one belonging to the search display controller and they are different (different adresses in memory as far as i observed)