1、直接看代码吧
//
// ViewController.m
#import "ViewController.h"
@interface ViewController () <UISearchBarDelegate, UITableViewDataSource, UITableViewDelegate>
{
UITableView * _tableView;
NSMutableArray * _dataArray;
NSMutableArray * _subDataArray;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor whiteColor];
[self configTableView];
UISearchBar * searchBar = [[UISearchBar alloc] init];
searchBar.frame = CGRectMake(0, 0, _tableView.frame.size.width, 0);
searchBar.delegate = self;
[searchBar sizeToFit]; //自动调整大小
_tableView.tableHeaderView = searchBar;
}
- (void) configTableView {
CGFloat width = [[UIScreen mainScreen] bounds].size.width;
CGFloat height = [[UIScreen mainScreen] bounds].size.height;
CGRect tableViewFrame = CGRectMake(0, 0, width, height);
_tableView = [[UITableView alloc] initWithFrame:tableViewFrame style:UITableViewStylePlain];
[self.view addSubview:_tableView];
_tableView.delegate = self;
_tableView.dataSource = self;
//备份数据源
_dataArray = [[NSMutableArray alloc] init];
for (int i = 0; i < 100; i++) {
[_dataArray addObject:[NSString stringWithFormat:@"%i", i]];
}
//数据源
_subDataArray = [[NSMutableArray alloc] initWithArray:_dataArray];
}
#pragma mark UISearchBarDelegate
//点击键盘上得search按钮 开始调用此方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
//清空数据源
[_subDataArray removeAllObjects];
NSString * getStr = searchBar.text;
//从备份数据源查找符合条件的数据,并加入到数据源中
for (NSString * str in _dataArray) {
if ([str containsString:getStr]) {
[_subDataArray addObject:str];
}
}
//更新UI
[_tableView reloadData];
//让键盘失去第一响应者
[searchBar resignFirstResponder];
}
#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _subDataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString * cellid = @"cellid";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellid];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellid];
}
cell.textLabel.text = _subDataArray[indexPath.row];
return cell;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
图:
2、实时显示搜索结果:
给searchBar添加如下属性:
searchBar.keyboardType = UIKeyboardTypeNumberPad;
searchBar.showsCancelButton = YES;
另外添加两个方法:
#pragma mark UISearchBarDelegate
//此方法实时监测搜索框中文本变化
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
NSString * getStr = searchBar.text;
[_subDataArray removeAllObjects];
if (getStr.length == 0) {
[_subDataArray addObjectsFromArray:_dataArray];
[_tableView reloadData];
}else {
//从备份数据源查找符合条件的数据,并加入到数据源中
for (NSString * str in _dataArray) {
if ([str containsString:getStr]) {
[_subDataArray addObject:str];
}
}
//更新UI
[_tableView reloadData];
}
}
//点击搜索框行的cancel按钮调用此方法
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
searchBar.text = @"";
[searchBar resignFirstResponder];
[_subDataArray removeAllObjects];
[_subDataArray addObjectsFromArray:_dataArray];
[_tableView reloadData];
}
图片:
3、搜索框提示信息
searchBar.prompt = @"请输入";
searchBar.placeholder = @"这是提示信息";
4、外观样式:
默认样式:
searchBar.barStyle = UIBarStyleDefault;
searchBar.barStyle = UIBarStyleBlack;
searchBar.barStyle = UIBarStyleBlack;
searchBar.translucent = YES;
黑色半透明:
指定颜色:
searchBar.tintColor = [UIColor orangeColor];
然后不显示,设置不成功,点进去看一下源码:
/*
The behavior of tintColor for bars has changed on iOS 7.0. It no longer affects the bar's background
and behaves as described for the tintColor property added to UIView.
To tint the bar's background, please use -barTintColor.
*/
@property(null_resettable, nonatomic,strong) UIColor *tintColor;
说明:ios7以后就不用了,要用barTintColor
searchBar.barTintColor = [UIColor orangeColor];
5、书签按钮:
//显示书签按钮
searchBar.showsBookmarkButton = YES;
#pragma mark UISearchBarDelegate
//相应书签按钮事件
- (void)searchBarBookmarkButtonClicked:(UISearchBar *)searchBar {
}