iOS开发UIKitiOS专题

UISearchController的简单实用

2015-11-13  本文已影响2443人  芝麻绿豆

UISearchController的简单介绍

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


效果预览

简单使用

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

+(instancetype)searchNavController:(UIViewController *)rootController{
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootController];
    navController.view.backgroundColor = YANColorBg;
    return [[self alloc] initWithSearchResultsController:navController];
}
self.tableView.tableHeaderView = self.searchController.searchBar;
self.searchController.searchResultsUpdater = self;
  -(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];
    }
}
  -(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];
        }
    }
}
-(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:@"网络请求失败"];
        } 
    }];
}
上一篇 下一篇

猜你喜欢

热点阅读