我正在处理的 iOS 应用程序中有一个表格视图,我想加载数据并仅在单击键盘上的搜索键时才开始搜索。目前,当我开始在搜索字段中输入时,它会加载数据,但是,由于有很多数据,这会产生很多延迟。
我尝试过在线查找,但大多数教程或示例都指的是立即加载数据或在用户在搜索栏中输入时加载数据。
任何建议将不胜感激,谢谢!
更新:已解决
[感谢 Tim & channi 的建议]
下面的代码是更新的解决方案:
- (void)filterContentForSearchText:(NSString*)searchText scope:(NSString*)scope
{
/* loads the file into a dictionary */
NSString *myFile=[[NSBundle mainBundle] pathForResource:@"test" ofType:@"plist"];
dict = [[NSDictionary alloc] initWithContentsOfFile:myFile];
recipes = [dict allKeys];
NSPredicate *resultPredicate = [NSPredicate
predicateWithFormat:@"SELF contains[cd] %@",
searchText];
searchResults = [recipes filteredArrayUsingPredicate:resultPredicate];
}
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
[self filterContentForSearchText:self.searchBar.text scope:[[self.searchDisplayController.searchBar scopeButtonTitles] objectAtIndex:[self.searchDisplayController.searchBar selectedScopeButtonIndex]]];
/* reloads the searchResults table view with the new data */
[self.searchDisplayController.searchResultsTableView reloadData];
[self.searchBar resignFirstResponder];
}
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
/* Sets both arrays to nil so main table view is a clean view */
searchResults = nil;
recipes = nil;
}