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, 否则会数据异常。
本文介绍了如何在iOS应用中正确使用UISearchDisplayController来实现搜索功能,包括加载配置、利用operationQueue处理大量搜索请求及确保数据更新时的正确刷新方式。

2万+

被折叠的 条评论
为什么被折叠?



