这是一个仿原生的搜索框,由于时间紧迫就简单的实现了一下功能。废话不多说,先上效果图。
下面上代码:
首先是搜索界面的.m
@interface ViewController ()
//搜索框/
@property (nonatomic, strong) UITextField *textField;
//展示视图的数组/
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
@implementation ViewController
-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
self.textField.text = @"";
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
self.navigationController.navigationBar.barTintColor = [UIColor brownColor];
self.navigationItem.titleView = self.textField;
}
#pragma mark -- UITtableViewdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return self.dataArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSString *cellIdentifier = @"cellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
cell.textLabel.text = self.dataArray[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.textField resignFirstResponder];
}
#pragma mark --
- (void)textFieldTextChange:(UITextField *)textField{
// 通过谓词匹配要查找的内容
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",self.textField.text];
// 查询结果数组
NSArray *array = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
JYTableViewController *resultTableViewController = [[JYTableViewController alloc]initWithStyle:UITableViewStylePlain];
// 将本界面数据传入到结果控制器
// 输入框内容
resultTableViewController.textFieldContent = textField.text;
// 结果数组
resultTableViewController.resultArray = array;
// 本界面展示数据的数组,传递此数据时为了在为了用来匹配结果控制器输入框的内容
resultTableViewController.dataArray = self.dataArray;
// 跳转
[self.navigationController pushViewController:resultTableViewController animated:NO];
}
#pragma mark -- lozy lode
-(UITextField *)textField{
if (!_textField) {
_textField = [[UITextField alloc]init];
_textField.placeholder = @" 搜索 ??";
_textField.tintColor = [UIColor purpleColor];
_textField.textAlignment = NSTextAlignmentLeft;
[_textField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
_textField.frame = CGRectMake(0, 0, screenWidth - 80, 44);
}
return _textField;
}
-(NSMutableArray *)dataArray{
if (!_dataArray) {
_dataArray = [NSMutableArray arrayWithCapacity:3];
for (NSInteger i = 0; i < 20; i++) {
[_dataArray addObject:[NSString stringWithFormat:@"BLACKSKY __ %ld",i]];
}
}
return _dataArray;
}
然后是结果控制器的.h文件
@interface JYTableViewController : UITableViewController
// 输入框的内容(将上界面输入框的内容传过来)
@property (nonatomic, copy) NSString *textFieldContent;
//*查询结果数据*/
@property (nonatomic, strong) NSArray *resultArray;
//*查询数据要从这里匹配*/
@property (nonatomic, strong) NSMutableArray *dataArray;
@end
下面是结果控制的.m文件
@interface JYTableViewController ()
//搜索框/
@property (nonatomic, strong) UITextField *textField;
@end
@implementation JYTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
// 添加基本界面
self.navigationItem.titleView = self.textField;
UIBarButtonItem *cancleButton = [[UIBarButtonItem alloc]initWithTitle:@"取消" style:UIBarButtonItemStylePlain target:self action:@selector(cancleButton)];
self.navigationItem.rightBarButtonItem = cancleButton;
self.navigationItem.hidesBackButton = YES;
[self.textField becomeFirstResponder];
self.textField.text = self.textFieldContent;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.resultArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *reuseIdentifier = @"reuseIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
}
cell.textLabel.text = self.resultArray[indexPath.row];
return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[self.textField resignFirstResponder];
}
#pragma mark --
- (void)cancleButton{
[self.textField resignFirstResponder];
[self.navigationController popViewControllerAnimated:NO];
}
- (void)textFieldTextChange:(UITextField *)textField{
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@",self.textField.text];
NSArray *array = [NSMutableArray arrayWithArray:[self.dataArray filteredArrayUsingPredicate:predicate]];
self.resultArray = array;
// 不要忘记刷新界面
[self.tableView reloadData];
// 当本输入框中的内容为空的时候返回
if (self.textField.text.length == 0) {
[self.textField resignFirstResponder];
[self.navigationController popViewControllerAnimated:NO];
}
}
#pragma mark -- lozy lode
-(UITextField *)textField{
if (!_textField) {
_textField = [[UITextField alloc]init];
// 这里懒省事就用设置一下占位,实际中可以设置 _textField.leftView和_textField.rightView来展现界面
_textField.placeholder = @" 搜索 ??";
_textField.tintColor = [UIColor purpleColor];
_textField.textAlignment = NSTextAlignmentLeft;
[_textField addTarget:self action:@selector(textFieldTextChange:) forControlEvents:UIControlEventEditingChanged];
_textField.frame = CGRectMake(0, 0, screenWidth - 80, 44);
}
return _textField;
}
@end
说句真心话,这些都是一点基础的东西。我明白它并不会能给被人带来什么帮助,但一定会给自己很多好处,无论是对iOS的喜好态度还是能力,thanks for。