使用UISearchController進行數(shù)據(jù)搜索

當UITableView擁有大量數(shù)據(jù)的時候,搜索功能變得十分重要。例如,聯(lián)系人列表。下面,我們就來做一個類似聯(lián)系人列表的搜索功能。
下圖是首頁:


Screen Shot 2016-06-27 at 21.14.31.png

單擊搜索框,輸入字符進行搜索:


Screen Shot 2016-06-27 at 21.14.56.png

輸入“T”,得到的結(jié)果:


Screen Shot 2016-06-27 at 21.15.15.png

關(guān)鍵代碼部分:
BNRAppDelegate.m
<code>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
if (!self.window.rootViewController) {
BNRItemsViewController *itemsViewController = [[BNRItemsViewController alloc] init];
// create UINavigationController obj
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:itemsViewController];
navController.restorationIdentifier = NSStringFromClass([navController class]);
self.window.rootViewController = navController;
}
[self.window makeKeyAndVisible];
return YES;
}
</code>
首頁顯示所有列表項:
BNRItemsViewController.m
<code>
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *nib = [UINib nibWithNibName:@"BNRItemCell" bundle:nil];
[self.tableView registerNib:nib forCellReuseIdentifier:@"BNRItemCell"];
self.tableView.restorationIdentifier = @"BNRItemsViewControllerTableView";
//create search controller
NSArray *items = [[BNRItemStore sharedStore] allItems];
BNRSearchResultController *resultController = [[BNRSearchResultController alloc] initWithItems:items];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:resultController];
self.searchController = [[UISearchController alloc] initWithSearchResultsController:navController];
self.definesPresentationContext = YES;
self.searchController.dimsBackgroundDuringPresentation = NO;
UISearchBar *searchBar = self.searchController.searchBar;
searchBar.delegate = self;
searchBar.placeholder = @"Enter a search name";
[searchBar sizeToFit];
self.tableView.tableHeaderView = searchBar;
self.searchController.searchResultsUpdater = resultController;
}
</code>
BNRSearchResultController 繼承UITableViewController,并且需要實現(xiàn)委托UISearchResultsUpdating。

BNRSearchResultController.m關(guān)鍵代碼:
<code>
//當輸入框內(nèi)容發(fā)送變化是,該方法被調(diào)用。使用predicateWithBlock進行關(guān)鍵字過濾,并且忽略大小寫。
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = searchController.searchBar.text;
[self.filteredItems removeAllObjects];
if (searchString.length > 0) {
NSPredicate *namePre = [NSPredicate predicateWithBlock:^BOOL(id _Nonnull evaluatedObject, NSDictionary<NSString *,id> * _Nullable bindings) {
NSRange range = [((BNRItem *)evaluatedObject).itemName rangeOfString:searchString options:NSCaseInsensitiveSearch];
return range.location != NSNotFound;
}];
self.filteredItems = [[self.items filteredArrayUsingPredicate:namePre] mutableCopy];
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
}

//當選擇搜索出來的結(jié)果時,導航到item詳細頁面。
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
BNRDetailViewController *detailViewController = [[BNRDetailViewController alloc] initForNewItem:NO];
BNRItem *selectedItem = self.filteredItems[indexPath.row];
detailViewController.item = selectedItem;
[self.presentingViewController.navigationController pushViewController:detailViewController animated:YES];
}
</code>
以上只是部分代碼。完整的項目,請到https://github.com/ryang420/Homepwner下載。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容