当UITableView拥有大量数据的时候,搜索功能变得十分重要。例如,联系人列表。下面,我们就来做一个类似联系人列表的搜索功能。
下图是首页:
Screen Shot 2016-06-27 at 21.14.31.png
单击搜索框,输入字符进行搜索:
Screen Shot 2016-06-27 at 21.14.56.png
输入“T”,得到的结果:
Screen Shot 2016-06-27 at 21.15.15.png
关键代码部分:
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,并且需要实现委托UISearchResultsUpdating。
BNRSearchResultController.m关键代码:
<code>
//当输入框内容发送变化是,该方法被调用。使用predicateWithBlock进行关键字过滤,并且忽略大小写。
-(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];
});
}
}
//当选择搜索出来的结果时,导航到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下载。
网友评论