废话少说直接上代码:
@interface JSLiveViewController ()<UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate, UISearchDisplayDelegate, JSProfessionScrollViewDelegate>
/**
* 搜索框
*/
@property (nonatomic, strong) UISearchBar *mySearchBar;
/**
* 搜索结果显示
*/
@property (nonatomic, strong) UISearchDisplayController *mySearchDisplayController;
//3.添加搜索框
[self setupMysearchBarAndMysearchDisPlay];
/**
* 设置tablview顶部搜索框
* 创建UISearchBar
* 创建UISearchDisplayController
*/
-(void)setupMysearchBarAndMysearchDisPlay
{
self.mySearchBar = [[UISearchBar alloc]init];
self.mySearchBar.placeholder = @"搜索老师或者房间号";
self.mySearchBar.delegate = self;
[self.mySearchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone];
[self.mySearchBar sizeToFit];
self.mySearchBar.backgroundColor = JSColor(200, 200, 200);
self.mySearchBar.backgroundImage = [UIImage imageWithColor:[UIColor clearColor] size:self.mySearchBar.bounds.size];
//加入列表的header里面
self.tableView.tableHeaderView = self.mySearchBar;
self.mySearchDisplayController = [[UISearchDisplayController alloc] initWithSearchBar:self.mySearchBar contentsController:self];
self.mySearchDisplayController.searchResultsTableView.rowHeight = self.tableView.rowHeight;
self.mySearchDisplayController.delegate = self;
self.mySearchDisplayController.searchResultsDataSource = self;
self.mySearchDisplayController.searchResultsDelegate = self;
}
/**
* 开始编辑时改变<取消>按钮的文字
*
* @param searchBar
*/
-(void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
self.mySearchBar.showsCancelButton = YES;
NSArray *subViews = [self.mySearchBar.subviews[0] subviews];
for (id view in subViews)
{
if ([view isKindOfClass:[UIButton class]])
{
UIButton* cancelbutton = (UIButton* )view;
[cancelbutton setTitle:@"取消" forState:UIControlStateNormal];
break;
}
}
}
/**
* 准备搜索前,把TableView调成全屏幕
*/
-(BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar
{
return YES;
}
/**
* 结束搜索时候恢复原状
*/
-(BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar
{
return YES;
}
/**
* SearchBar输入内容有变化的时候,则执行此方法,询问要不要重装searchResultTableView的数据
* Return YES to cause the search result table view to be reloaded.
*/
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString
{
//根据用户的输入的关键字searchString,处理出搜索结果(可以是本地搜索,可以是从服务器请求过来的数据)到数组
[self filterContentForSearchText:searchString];
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 0.001);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
for (UIView* v in self.mySearchDisplayController.searchResultsTableView.subviews)
{
if ([v isKindOfClass: [UILabel class]] && [[(UILabel*)v text] isEqualToString:@"No Results"])
{
UILabel *lbl = (UILabel*)v;
lbl.text = @"没有需要显示的结果";
break;
}
}
});
return YES;
}
/**
* 如果设置了选项,当Scope Button选项有变化的时候,则执行此方法,询问要不要重装searchResultTableView的数据
* Return YES to cause the search result table view to be reloaded.
*/
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption
{
[self filterContentForSearchText:self.mySearchBar.text];
return YES;
}
/**
* 是否包含或等于要搜索的字符串内容
*
* @param searchText searchText 要查找的内容
* @param scope 范围
*/
-(void)filterContentForSearchText:(NSString*)searchText
{
NSMutableArray *tempResults = [NSMutableArray array];
//不区分大小写 | 忽略 "-" 符号的比较
NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch;
for (int i = 0; i < self.liveCellVoMutableArray.count; i++)
{
JSLiveCellVo *storeCellVo = [self.liveCellVoMutableArray objectAtIndex:i];
//教师名称查找
NSString *storeString = [[self.liveCellVoMutableArray objectAtIndex:i] teacherName];
NSRange storeRange = NSMakeRange(0, storeString.length);
NSRange foundRange = [storeString rangeOfString:searchText options:searchOptions range:storeRange];
//班级名称查找
NSString *roomStr = [[self.liveCellVoMutableArray objectAtIndex:i] courseRoom];
NSRange roomRange = NSMakeRange(0, roomStr.length);
NSRange foundRoomRange = [roomStr rangeOfString:searchText options:searchOptions range:roomRange];
if (foundRange.length || foundRoomRange.length)
{
[tempResults addObject:storeCellVo];
}
}
[self.searchResultsData removeAllObjects];
[self.searchResultsData addObjectsFromArray:tempResults];
}