私は、UISearchDisplayControllerとUITableViewを使用して検索結果を表示するSearchBarを実装しました。私はlibxml2とxpathを使ってHTML Webサイトを解析し、ソースコードに必要なコンテンツを検索しています。私はObjCの新人であるため、Appleが提供するサンプルプロジェクトのTableSearchを使用して、検索と表示の一部を開始しました。すべてこれでうまく動作し、私はウェブサイトから特定のコンテンツを解析し、それらがウェブサイトに表示されているように正しく結合し、それらを異なるTableViewの行のビューに表示させることができます。私はそれのために特定のウェブサイトを検索するためにユーザー入力を使用したい。プロジェクトTableSearch(クラスMainViewController.m)を見てみましょう場合、あなたはそれが「filteredListContent」を更新し、ユーザーの種類として自動的に表示するテーブルビューを再ロードすることがわかりますUISearchBarとUITableViewを使用したUISearchDisplayController - 「ライブ結果」を避けるにはどうすればよいですか?
:私は次のような問題を持っている
[...]
#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
私の実装を解析と検索に使用するときにはメモリが必要であることは想像できますが、「ライブ結果」を表示するためにユーザーが入力中に繰り返し呼び出すと特に重大です。ブロックの最初の行だけを解析して検索すると(NSDataオブジェクトをURLで初期化する)、SearchBarが遅れて開始し、各文字が入力された後数秒間遅延します。ブロック全体を使用すると、アプリケーションがクラッシュします。
検索が実行される前にキーボードの「検索」ボタンまたは「戻る」ボタンがタップされるのを待つ方法、またはボタンがタップされたかどうかを確認する方法はありますか?この恐らく簡単な質問には申し訳ありません。