我已经实现了一个使用UISearchDisplayController和UITableView来显示搜索结果的SearchBar。我使用libxml2和xpath来解析HTML网站并搜索源代码中所需的内容。由于我是ObjC的新手,因此我使用Apple提供的示例项目TableSearch作为搜索和显示部分。一切工作正常,我可以从网站解析特定的内容,并将它们正确地组合,因为它们出现在网站上,并让它们显示在不同的TableView行视图中。我想使用用户输入来搜索特定的网站。如果你看看在项目TableSearch(类MainViewController.m)UISearchDisplayController与UISearchBar和UITableView - 如何避免“现场结果”?
,你会发现,它更新了“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
您可以想象,当我使用我的实现进行解析和搜索时,它需要一些内存,当用户输入以显示“实时结果”时,它被重复调用时尤其重要。当我仅使用块的第一行进行解析和搜索(使用URL初始化NSData对象)时,SearchBar会启动滞后,并在输入每个字符后延迟几秒。当我使用整块时,应用程序崩溃。我的问题如下:
如何在执行搜索之前等待按键盘上的“搜索”或“返回”按钮或在哪里以及如何检查该按钮是否被轻敲?对不起,这个可能微不足道的问题。