UISearchDisplayController与UISearchBar和UITableView - 如何避免"实时结果"?

Joh*_*ohn 1 iphone cocoa-touch objective-c uisearchdisplaycontroller

我使用UISearchDisplayController和UITableView实现了一个SearchBar来显示搜索结果.我使用libxml2和xpath来解析HTML网站并搜索源代码中所需的内容.由于我是ObjC的新手,我使用Apple提供的示例项目TableSearch作为搜索和显示部分作为开​​始.一切正常,我可以解析网站上的特定内容,并将它们正确地组合在网站上,并将它们显示在不同的TableView行视图中.我想使用用户输入来搜索特定的网站.我只有以下问题:

如果您查看项目TableSearch(类MainViewController.m),您会注意到它更新了"filteredListContent"并重新加载TableView,并在用户输入时自动显示它:

[...]

#pragma mark -
#pragma mark Content Filtering

- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
 /*
  Update the filtered array based on the search text and scope.
  */

 [self.filteredListContent removeAllObjects]; // First clear the filtered array.

 /*
  Search the main list for products whose type matches the scope (if selected) and whose name matches searchText; add items that match to the filtered array.
  */
 for (Product *product in listContent)
 {
  if ([scope isEqualToString:@"All"] || [product.type isEqualToString:scope])
  {
   NSComparisonResult result = [product.name compare:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch) range:NSMakeRange(0, [searchText length])];
            if (result == NSOrderedSame)
   {
    [self.filteredListContent addObject:product];
            }
  }
 }
}


#pragma mark -
#pragma mark UISearchDisplayController Delegate Methods

- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
    [self filterContentForSearchText:searchString scope:
   [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}


- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text] scope:
   [[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:searchOption]];

    // Return YES to cause the search result table view to be reloaded.
    return YES;
}


@end
Run Code Online (Sandbox Code Playgroud)

你可以想象当我使用我的实现进行解析和搜索时需要一些内存,当用户输入以便显示"实时结果"时重复调用它时尤其重要.当我只使用我的块的第一行进行解析和搜索(用URL初始化NSData对象)时,SearchBar开始滞后并在每个字符键入后延迟几秒钟.当我使用整个块时,应用程序崩溃了.我的问题如下:

如何在执行搜索之前等待键盘上的"搜索"或"返回"按钮,或者在何处以及如何检查按钮是否被点击?对不起这个可能微不足道的问题.

ugh*_*fhw 6

使您的委托对象也成为搜索栏的委托,并按如下方式实现这些方法:

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self filterContentForSearchText:[self.searchDisplayController.searchBar text]
                               scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString {
    return NO;
}
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption {
    return NO;
}
Run Code Online (Sandbox Code Playgroud)

这将阻止在用户键入或更改范围时重新加载表视图,并在单击搜索按钮时重新加载.但是,在输入第一个字符时,表会重新加载.我还没有找到防止这种行为的方法.实际上,当输入第一个字符时,表可能会说没有结果.