UISearchDisplayController と UITableView を使用して SearchBar を実装し、検索結果を表示しました。 libxml2 と xpath を使用して HTML Web サイトを解析し、ソース コードで必要なコンテンツを検索しています。私は ObjC を初めて使用するので、Apple が提供するサンプル プロジェクトの TableSearch を使用して検索および表示部分を開始しました。これですべてが正常に機能します。Web サイトから特定のコンテンツを解析し、Web サイトに表示されるとおりにそれらを正しく組み合わせて、異なる TableView 行のビューに表示することができます。ユーザー入力を使用して、特定の Web サイトを検索したいと考えています。次の問題しかありません。
プロジェクト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
私の実装を解析と検索に使用する場合、ある程度のメモリが必要であることが想像できます。また、ユーザーが「ライブ結果」を表示するために入力中に繰り返し呼び出される場合は特に重要です。ブロックの最初の行のみを解析と検索 (URL を使用して NSData オブジェクトを初期化) に使用すると、SearchBar が遅れ始め、各文字が入力された後に数秒遅れます。ブロック全体を使用すると、アプリがクラッシュします。私の質問は次のとおりです。
検索が実行される前に、キーボードの「検索」または「戻る」ボタンがタップされるのをどのように待つことができますか、またはボタンがタップされたかどうかをどこでどのように確認できますか? このおそらく些細な質問で申し訳ありません。