项目介绍:
模仿微信的搜索框效果,自定义SearchController,实现了和系统默认的UISearchController相同的使用方式和代理回调。
和UISearchController相同的初始化方法:
[Actionscript3] 查看源文件 复制代码
- (JKRSearchController *)searchController { if (!_searchController) { JKRSearchResultViewController *resultSearchController = [[JKRSearchResultViewController alloc] init]; _searchController = [[JKRSearchController alloc] initWithSearchResultsController:resultSearchController]; _searchController.searchBar.placeholder = @"搜索"; _searchController.hidesNavigationBarDuringPresentation = YES; _searchController.searchResultsUpdater = self; _searchController.searchBar.delegate = self; _searchController.delegate = self; } return _searchController; }
和UISearchController相同的加载方式:
[Actionscript3] 查看源文件 复制代码
[self.tableView setTableHeaderView:self.searchController.searchBar];
和UISearchController相同的代理回调:
[Actionscript3] 查看源文件 复制代码
#pragma mark - JKRSearchControllerhResultsUpdating - (void)updateSearchResultsForSearchController:(JKRSearchController *)searchController { NSString *searchText = searchController.searchBar.text; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(SELF CONTAINS %@)", searchText]; JKRSearchResultViewController *resultController = (JKRSearchResultViewController *)searchController.searchResultsController; if (!(searchText.length > 0)) resultController.filterDataArray = @[]; else resultController.filterDataArray = [self.dataArray filteredArrayUsingPredicate:predicate]; } #pragma mark - JKRSearchControllerDelegate - (void)willPresentSearchController:(JKRSearchController *)searchController { NSLog(@"willPresentSearchController, %@", searchController); } - (void)didPresentSearchController:(JKRSearchController *)searchController { NSLog(@"didPresentSearchController, %@", searchController); } - (void)willDismissSearchController:(JKRSearchController *)searchController { NSLog(@"willDismissSearchController, %@", searchController); } - (void)didDismissSearchController:(JKRSearchController *)searchController { NSLog(@"didDismissSearchController, %@", searchController); } #pragma mark - JKRSearchBarDelegate - (void)searchBarTextDidBeginEditing:(JKRSearchBar *)searchBar { NSLog(@"searchBarTextDidBeginEditing %@", searchBar); } - (void)searchBarTextDidEndEditing:(JKRSearchBar *)searchBar { NSLog(@"searchBarTextDidEndEditing %@", searchBar); } - (void)searchBar:(JKRSearchBar *)searchBar textDidChange:(NSString *)searchText { NSLog(@"searchBar:%@ textDidChange:%@", searchBar, searchText); }
Demo地址:https://github.com/Joker-388/JKRCustomSearchController