UISearchBar和UISearchDisplayController配合使用,是iOS8之前的使用方法,iOS8以后使用UISearchController,更方便簡單。
遵守協議<UISearchBarDelegate, UISearchDisplayDelegate>
@property(nonatomic,strong) UISearchBar *searchBar;
@property(nonatomic,strong) UISearchDisplayController *searchDisplayController;
@property(nonatomic,strong) NSMutableArray *searchResultArray;//搜索結果數組
初始化控件
#pragma mark - getter
- (UISearchBar*)searchBar {
if(!_searchBar) {
_searchBar= [[UISearchBar alloc]init];
_searchBar.delegate=self;
_searchBar.tintColor= [YFRUtility colorFromHexString:@"0x3f75ff"];//設置bar字體前景色
_searchBar.barTintColor= [YFRUtility colorFromHexString:@"0xEDEDF3"];//bar前景色
_searchBar.layer.borderColor= [YFRUtility colorFromHexString:@"0xEDEDF3"].CGColor;
_searchBar.layer.borderWidth=0.5;//為了掩飾黑線,加邊框設置前景色同色
[_searchBar sizeToFit];
}
return _searchBar;
}
- (UISearchDisplayController*)searchDisplayController {
if(!_searchDisplayController) {
_searchDisplayController= [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
_searchDisplayController.delegate=self;
_searchDisplayController.searchResultsDataSource=self;
_searchDisplayController.searchResultsDelegate=self;
_searchDisplayController.searchResultsTableView.tableFooterView= [UIView new];//無數據時不顯示cell
}
return_searchDisplayController;
}
viewDidLoad代碼
- (void)viewDidLoad {
[super viewDidLoad];
self.searchResultArray = [NSMutableArray array];
self.tableView.tableHeaderView = self.searchBar;
self.edgesForExtendedLayout=UIRectEdgeNone;//解決頁面跳轉時searchBar跳動問題
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
//跳轉頁面時searchDisplayController置為不活躍
if(_searchDisplayController.isActive) {
_searchDisplayController.active=NO;
}
}
實現UISearchBar的代理回調方法
#pragma mark - UISearchBar Delegate
//實時搜索
- (void)searchBar:(UISearchBar*)searchBar textDidChange:(NSString*)searchText {
[self searchDataWithKeyword:searchText];//執行搜索代碼
}
//點擊搜索按鈕
- (void)searchBarSearchButtonClicked:(UISearchBar*)searchBar {
[self searchDataWithKeyword:searchBar.text];//執行搜索代碼
}
//取消搜索
- (void)searchBarCancelButtonClicked:(UISearchBar*)searchBar {
[self.searchResultArray removeAllObjects];//取消搜索清空result數組
}
搜索數據的方法
- (void)searchDataWithKeyword:(NSString*)keyword {
[self.searchResultArray removeAllObjects];//清空之前的數據
//網絡請求或本地搜索
{
//self.searchResultArray賦值并更新結果列表
[_searchDisplayController.searchResultsTableView reloadData];
}
}
//解決tableView下拉刷新沖突
#pragma mark UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView*)scrollView {
if(_searchDisplayController.isActive) {
return;
}
...//下拉刷新代碼
}
Tableview的回調:區分原始tableview和resultTableview
#pragma mark -- UITableViewDelegate
-(NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section{
if(_searchDisplayController.isActive) {//搜索結果
return [self.resultArray count];
}else{//原始tableview
return [self.dataSource count];
}
}
- (CGFloat)tableView:(UITableView*)tableView heightForRowAtIndexPath:(NSIndexPath*)indexPath
{
if(_searchDisplayController.isActive) {//搜索結果
return 44;
}else{//原始tableview
return 60;
}
}
-(UITableViewCell*)tableView:(UITableView*)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath{
staticNSString*identifier =@"";
UITableViewCell*cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if(!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
if(_searchDisplayController.isActive) {//搜索結果
}else{//原始tableview
}
returncell;
}
-(void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
if(_searchDisplayController.isActive) {//搜索結果
}else{//原始tableview
}
}