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

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

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

關(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下載。