0

问题的简短说明: 我想设置搜索栏的文本,而不自动触发与其绑定的搜索显示控制器。searchdisplaycontroller:更改搜索栏的文本

问题的详细描述: 我有一个带有搜索栏和搜索显示控制器的iphone应用程序。 searchdisplaycontroller用于自动完成。对于自动填充,我使用了一个sqlite数据库。用户输入关键字的前几个字母,结果显示在searchdisplaycontroller的表格中。每个输入的字符都会执行一个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; 
} 
 

请问我是否不清楚我的问题是什么。我需要你的帮助。谢谢

回答

1

没有尝试: mainViewController.searchDisplayController.searchBar.text=selectedKeyword;

您可以从搜索控制器访问搜索栏并更新其文本内容。

+0

仅供参考:'searchDisplayController'在iOS 8中已弃用。请查看'UISearchController' – Besi 2015-06-12 13:17:51

0

现在它的工作我不知道发生了什么。我想我在更改文字后试图关闭搜索显示控制器。无论如何感谢您的帮助。我想我也试图改变原有的文本框不是一个属于搜索显示控制器和他们是不同的(在内存中的不同不会忽略据我观察)

 

- (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; 

     [[mainViewController searchController] setActive:NO animated: YES]; 

     [mainViewController.searchController.searchBar setText: self.selectedKeyword]; 

    } 
}