UISearchBar+UItableView组合使用
不多说,上代码,直接拿来用就行了,别客气哈。。。
#import "BaseSearchViewController.h"
#define cellIdentify @"cellReuse"
@interface BaseSearchViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate>
@property(nonatomic, strong) UITableView *tableView;
@property(nonatomic, strong) UISearchBar *searchBar;
@property (nonatomic,strong) UIButton *backgroundView;//淡黑色半透明遮盖view
@property(nonatomic, strong) NSString *placeHolder;
@property(nonatomic, assign) BOOL dismissAfterSeleted;//选择后,退出搜索
@property(nonatomic, assign) BOOL shouldShowSearchReaults;
@property (nonatomic,strong)NSMutableArray *searchArrays;
@end
@implementation BaseSearchViewController
- (NSMutableArray *)searchArrays {
if (!_searchArrays) {
_searchArrays = [NSMutableArray array];
}
return _searchArrays;
}
- (UIButton *)backgroundView {
if (!_backgroundView) {
_backgroundView =[[UIButton alloc]initWithFrame:self.view.bounds];
[_backgroundView addTarget:self action:@selector(cancelSearch) forControlEvents:UIControlEventTouchUpInside];
_backgroundView.backgroundColor =[UIColor blackColor];
_backgroundView.alpha =0.3;
}
return _backgroundView;
}
- (UISearchBar *)searchBar {
if (!_searchBar) {
_searchBar = [[UISearchBar alloc] init];
_searchBar.delegate = self;
_searchBar.placeholder = @"search here ...";
[_searchBar sizeToFit];
_searchBar.showsCancelButton = YES;
}
return _searchBar;
}
#pragma mark - setter 方法
- (void)setShouldShowSearchReaults:(BOOL)shouldShowSearchReaults {
_shouldShowSearchReaults = shouldShowSearchReaults;
self.tableView.hidden = !shouldShowSearchReaults;
if (shouldShowSearchReaults) {
[self.view bringSubviewToFront:self.tableView];
[self.tableView reloadData];
}else {
[self.backgroundView removeFromSuperview];
}
self.searchBar.showsCancelButton = shouldShowSearchReaults;
}
- (void)setPlaceHolder:(NSString *)placeHolder {
if (_placeHolder != placeHolder) {
_placeHolder = placeHolder;
self.searchBar.placeholder = placeHolder;
}
}
#pragma mark - view
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
}
- (void)initView {
self.view.backgroundColor = [UIColor whiteColor];
self.shouldShowSearchReaults = false;
CGRect frame = self.view.bounds;
CGRect rect = [[UIApplication sharedApplication] statusBarFrame];
CGFloat height = rect.size.height;
CGFloat navigationheight = self.navigationController.navigationBar.frame.size.height;
CGRect bframe = self.searchBar.frame;
bframe.origin.y += (height + navigationheight);
self.searchBar.frame = bframe;
[self.view addSubview:self.searchBar];
frame.origin.y += (CGRectGetHeight(bframe) + CGRectGetMinY(bframe));
frame.size.height -= CGRectGetMinY(frame);
self.tableView = [[UITableView alloc] initWithFrame:frame];
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:cellIdentify];
self.tableView.tableFooterView = [UIView new];
[self.view addSubview:self.tableView];
[self.tableView reloadData];
self.tableView.hidden = !self.shouldShowSearchReaults;
self.backgroundView.frame = frame;
}
//点击遮罩,改变搜索状态
-(void)cancelSearch {
[self searchBarCancelButtonClicked:self.searchBar];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.searchArrays.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
}
cell.textLabel.text = sqlite.buildName;
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[self.searchBar resignFirstResponder];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - search Bar
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSString *filterString = searchBar.text;
if (filterString.length <= 0) {
return;
}
[self.backgroundView removeFromSuperview];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
if (!self.shouldShowSearchReaults) {
self.shouldShowSearchReaults = YES;
}
[self.backgroundView removeFromSuperview];
[searchBar resignFirstResponder];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
[self.searchArrays removeAllObjects];
self.shouldShowSearchReaults = false;
[self.searchBar resignFirstResponder];
searchBar.text = @"";
}
- (BOOL)searchBarShouldBeginEditing:(UISearchBar *)searchBar {
[self.searchArrays removeAllObjects];
self.shouldShowSearchReaults = YES;
if (self.shouldShowSearchReaults) {
[self.view addSubview:self.backgroundView];
}
return YES;
}
- (BOOL)searchBarShouldEndEditing:(UISearchBar *)searchBar {
[self.searchBar resignFirstResponder];
return YES;
}
#pragma mark - 直接读取
- (CGRect)searchBarFrame {
return self.searchBar.frame;
}
@end