UISearchController的简单实用

UISearchController的简单介绍

UISearchController是iOS8开始实用的搜索框,iOS8以前是UISearchBar和UISearchDispalyController结合使用。
UISearchController继承UIViewController,同时结合UINavigationController和UITableViewController实现搜索功能。


UISearchController的简单实用_第1张图片
效果预览

简单使用

创建显示结果的tablview,将tableview添加到导航控制器的栈顶控制器:

  • 自定义类方法初始化UISearchController:
    rootController为显示结果的UITableViewController
+(instancetype)searchNavController:(UIViewController *)rootController{
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController];
    navController.view.backgroundColor = YANColorBg;
    return [[self alloc] initWithSearchResultsController:navController];
}
  • 添加searchBar
self.tableView.tableHeaderView = self.searchController.searchBar;
UISearchController的简单实用_第2张图片
  • 设置代理
self.searchController.searchResultsUpdater = self;
  • 实现代理方法:
    • 点击搜索框调用此方法;
    • 获取搜索框输入的文字
    • 将遍历的结果赋值到栈顶的tableview显示结果
  -(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
      NSString *searchText = searchController.searchBar.text;
      YANLog(@"%@",searchText);
      [self updateFilteredContentForName:searchText];
      if (self.searchController.searchResultsController) {
        // 设置显示搜索结果的tableView
        UINavigationController *nav = (UINavigationController *)self.searchController.searchResultsController;
        YANSearchTableController *searchTable = (YANSearchTableController *)nav.topViewController;
        searchTable.tags = self.searchResult;
        [searchTable.tags insertObject:searchText atIndex:0];
        [searchTable.tableView reloadData];
    }
}
  • 遍历结果集的函数
    • self.tags :网络获取的数据存储在对应的模型
    • self.searchResult:将从模型中遍历出的结果存在临时的数组中
  -(void)updateFilteredContentForName:(NSString *)tagName{
     if (tagName == nil || tagName.length == 0) {
        NSMutableArray *searchResult = [NSMutableArray array];
        self.searchResult = searchResult;
        return;
    }
    // 移除之前的查询结果
    [self.searchResult removeAllObjects];
    // 遍历模型数据
    for (YANTag *tag in self.tags) {
        NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
        NSRange productNameRange = NSMakeRange(0, tag.theme_name.length);
        NSRange foundRange = [tag.theme_name rangeOfString:tagName options:searchOptions range:productNameRange];
        if (foundRange.length > 0) {
            [self.searchResult addObject:tag];
        }
    }
}
UISearchController的简单实用_第3张图片
  • 栈顶控制器YANSearchTableController的设置
  • UISearchController的searchBar的代理方法和属性设置
  • 设置searcherBar的介绍请参考上篇文章
  • 点击search的代理方法实现网络请求获取查询数据
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    NSString *searchText = searchBar.text;
    NSString *url = @"http://api.budejie.com/api/api_open.php";
    NSDictionary *params = @{
                             @"a":@"tag_search",
                             @"c":@"topic",
                             @"kw":searchText
                             };
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:url parameters:params success:^(NSURLSessionDataTask * _Nonnull task, id  _Nonnull responseObject) {
        if (responseObject == nil) {
            [SVProgressHUD showErrorWithStatus:@"数据加载失败"];
        }
        UINavigationController *nav = (UINavigationController *)self.searchResultsController;
        YANSearchTableController *tableController = (YANSearchTableController *)nav.topViewController;
        tableController.tags = [YANTag objectArrayWithKeyValuesArray:responseObject];
        [tableController.tags insertObject:searchText atIndex:0];
        [tableController.tableView reloadData];
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nonnull error) {
        if (error.code == NSURLErrorCancelled) return;
        if (error.code == NSURLErrorTimedOut) {
            [SVProgressHUD showErrorWithStatus:@"网络请求超时,请稍后再试"];
        }else{
            [SVProgressHUD showErrorWithStatus:@"网络请求失败"];
        } 
    }];
}
UISearchController的简单实用_第4张图片
UISearchController的简单实用_第5张图片

你可能感兴趣的:(UISearchController的简单实用)