1
我正在iPad上使用分屏視圖控制器實現搜索功能。它搜索我從JSON對象獲得的NSDictionary
數組。iOS UISearchBar如何正確實現「按照您的搜索類型」行爲?
我在表格視圖的標題視圖中添加了一個搜索欄,並使用下面的代碼隨着用戶在搜索欄中輸入搜索查詢來更新帶有搜索結果的表格。
我很感興趣如果我正在做的是在iOS上實現「鍵入時搜索」行爲的正確方法?如果我的JSON返回1000個結果,我會遇到性能問題嗎?我是否需要實現搜索結果控制器,而不是將搜索欄放入表視圖標題中?
-(void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
[self doSearchWithString:searchText];
}
- (void)doSearchWithString:(NSString*)searchString
{
NSPredicate* predicate = [NSPredicate predicateWithBlock:^BOOL(NSDictionary *object, NSDictionary *bindings) {
//get the value from the dictionary (the name value)
NSString *name = [object objectForKey:@"name"];
if([[name lowercaseString] rangeOfString:[searchString lowercaseString]].location != NSNotFound) {
return YES;
}
return NO;
}];
//change the datasource and ask the table to refresh itself
self.searchResults = [self.dataSource filteredArrayUsingPredicate:predicate];
[self.tableView reloadData];
}