-
创建UItableViewVC
-
UISearchController alloc initResultsController 一个tableVC,搜索到的数据源呈现在tableVC -
继承UITableViewController .h文件
@interface TestTableViewController : UITableViewController @end -
继承UITableViewController .m文件
@interface TestTableViewController ()<UISearchResultsUpdating, UISearchBarDelegate> @property (nonatomic, strong) UISearchController *searchController; @property (nonatomic, strong) NSMutableArray *searchResults; // Filtered search results @property (nonatomic, strong) NSArray *products; @end @implementation TestTableViewController - (void)viewDidLoad{ [super viewDidLoad]; self.title = @"全部品牌"; self.products = [Product allProducts]; TestResultTableViewController *result = [[TestResultTableViewController alloc] init]; result.view.frame = CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height); self.searchController = [[UISearchController alloc] initWithSearchResultsController:result]; self.searchController.searchResultsUpdater = self; self.searchController.searchBar.placeholder = @"搜索品牌"; self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0); self.tableView.tableHeaderView = self.searchController.searchBar; self.definesPresentationContext = YES; } -
UISearchResultsUpdating UISearchBarDelegate 的代理
#pragma mark - UISearchResultsUpdating - (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ NSString *searchString = [self.searchController.searchBar text]; NSString *scope = nil; NSInteger selectedScopeButtonIndex = [self.searchController.searchBar selectedScopeButtonIndex]; if (selectedScopeButtonIndex > 0) { scope = [[Product deviceTypeNames] objectAtIndex:(selectedScopeButtonIndex - 1)]; } [self updateFilteredContentForProductName:searchString type:scope]; if (self.searchController.searchResultsController) { TestResultTableViewController *vc = (TestResultTableViewController *)self.searchController.searchResultsController; vc.searchResults = self.searchResults; [vc.tableView reloadData]; } } #pragma mark - Content Filtering - (void)updateFilteredContentForProductName:(NSString *)productName type:(NSString *)typeName{ if ((productName == nil) || [productName length] == 0) { if (typeName == nil) { self.searchResults = [self.products mutableCopy]; } else { NSMutableArray *searchResults = [[NSMutableArray alloc] init]; for (Product *product in self.products) { if ([product.type isEqualToString:typeName]) { [searchResults addObject:product]; } } self.searchResults = searchResults; } return; } [self.searchResults removeAllObjects]; for (Product *product in self.products) { if ((typeName == nil) || [product.type isEqualToString:typeName]) { NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch; NSRange productNameRange = NSMakeRange(0, product.name.length); NSRange foundRange = [product.name rangeOfString:productName options:searchOptions range:productNameRange]; if (foundRange.length > 0) { [self.searchResults addObject:product]; } } } } #pragma mark - UISearchBarDelegate - (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{ [self updateSearchResultsForSearchController:self.searchController]; } -
代码


