UISearchDisplayController 异步搜索

本文介绍了如何在iOS应用中正确使用UISearchDisplayController来实现搜索功能,包括加载配置、利用operationQueue处理大量搜索请求及确保数据更新时的正确刷新方式。

1. 加载 UISearchDisplayController

_searchBar.delegate = self;
    [_searchBar setPlaceholder:@"搜索"];
    [_searchBar setSearchFieldBackgroundImage:ImageN(@"search_background") forState:(UIControlStateNormal)];
    
    _tableList.tintColor = kColorMainTint;
    
    _searchController = [[UISearchDisplayController alloc]initWithSearchBar:_searchBar contentsController:self];
    _searchController.active = NO;
    _searchController.searchResultsDataSource = self;
    _searchController.searchResultsDelegate = self;
    _searchController.delegate = self;
    _searchController.searchResultsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;


2. 在searchBarDelegate  回调 - (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText 中处理,

     我是使用operationQueue ,在搜索源比较大的情况下 ,每输入一次就new 一个 operation 放到线程处理,


3. 重点:线程处理完后要切换到主线程刷新 talbleview, 这个时候一定要 reload  searchDisplayController.searchResultsTableView 而不是非搜索的 tableview, 否则会数据异常。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值