UISearchController进行网络实时搜索

这篇博客介绍了如何利用UISearchController进行网络实时搜索。首先设置搜索button触发搜索,然后创建一个UITableView来展示搜索结果。在UISearchController中设置搜索结果更新器,并在用户输入时发送POST请求到服务器接口,获取数据并实时更新表格视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先设置一个搜索button用以触发搜索

self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(searchBarButtonItemAction:)];


接下来到方法中进行

- (void)searchBarButtonItemAction:(UIButton *)sender {

    self.tableViewVC = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];

    self.tableViewVC.tableView.frame = CGRectMake(0, 0, kWidth, kHeight - 49 - 64);

    _tableViewVC.tableView.tableFooterView =[[UIView alloc] initWithFrame:CGRectZero];

    _tableViewVC.tableView.delegate = self;

    _tableViewVC.tableView.dataSource = self;

    _tableViewVC.tableView.rowHeight = 60;

    [_tableViewVC.tableView registerClass:[SearchCell class] forCellReuseIdentifier:@"search"];

    

    self.searchVC = [[UISearchController alloc] initWithSearchResultsController:self.tableViewVC];

    self.searchVC.delegate = self;

    _searchVC.searchResultsUpdater = self;

    [_searchVC setHidesNavigationBarDuringPresentation:NO];

    _searchVC.searchBar.placeholder = @"搜索新闻/用户/公司";

    self.tabBarController.tabBar.hidden = YES;


    [self presentViewController:self.searchVC animated:YES completion:nil];

}




UISearchController的创建需要指定一个展示搜索结果的控制器, 如果填nil默认为当前控制器. 在实际应用中, 当前控制器并不适合展示结果, 所以新建了一个tableview展示结果. 
如果当前控制器的导航栏是不透明的, 需要设置
[_searchVC setHidesNavigationBarDuringPresentation:NO]; 
否则searchBar会显示不完全. 如果当前控制器的导航栏是透明的, searchBar会正常显示, 但是会导致滚动试图有时莫名其妙缩进, 我还没搞明白, 下望大家交流下.


点击搜索后, 会弹出seachBar, 在searchBar输入要搜索的关键字, 这时下面这个方法就会执行, 当然要先指定

_searchVC.searchResultsUpdater =self;

我以向服务器接口发送post请求为例. 你每输入一个字或一个字母他就会执行一次下面的方法, 所以搜索数据就会实时展示

#pragma mark - UISearchResultsUpdating


- (void)updateSearchResultsForSearchController:(UISearchController *)searchController

{

    self.searchKey = searchController.searchBar.text;

    NSMutableURLRequest *requirest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://接口网址"]];

    NSString *value =[NSString stringWithFormat:@"word=%@", self.searchKey];

    requirest.HTTPBody = [value dataUsingEncoding:NSUTF8StringEncoding];

    requirest.HTTPMethod = @"POST";

    

    [NSURLConnection sendAsynchronousRequest:requirest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {

        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];

        self.result = [[Result alloc] initWithDictionary:dic[@"data"]];

        [self.tableViewVC.tableView reloadData];

    }];

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值