头文件:
#ifndef SearchWindowViewController_h
#define SearchWindowViewController_h
#import <UIKit/UIKit.h>
@interface SearchWindowViewController : UIViewController<UITableViewDelegate, UITableViewDataSource>
@property(nonatomic, retain) UITableView *tv;
@property(nonatomic, retain) UITextField *tf;
@end
#endif /* SearchWindowViewController_h */
.m文件:
#import <Foundation/Foundation.h>
#import "SearchWindowViewController.h"
@interface SearchWindowViewController()<UITextFieldDelegate>
// 搜索前的原始数据数组
@property(nonatomic, retain) NSMutableArray *original;
// 搜索时输入的文字
@property(nonatomic, retain) NSString *search;
@end
@implementation SearchWindowViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initView];
}
-(void) initView{
// 隐藏默认左上角返回按钮
self.navigationItem.leftBarButtonItem = nil;
self.navigationItem.hidesBackButton = YES;
_tf=[[UITextField alloc]init];
//设置frmae
_tf.width=370;
_tf.height=35;
_tf.backgroundColor=[UIColor whiteColor];
_tf.placeholder = @"检索站内信";
_tf.layer.cornerRadius = 9;
_tf.layer.masksToBounds = YES;
//设置文字内容垂直居中
_tf.contentVerticalAlignment=UIControlContentVerticalAlignmentCenter;
[_tf addTarget:self action:@selector(searchMethod) forControlEvents:UIControlEventEditingChanged];
//设置左边的放大镜
UIImageView *leftView=[[UIImageView alloc]init];
//设置leftView的frame
leftView.width=30;
leftView.height=30;
leftView.image=[UIImage imageNamed:@"searchbar_icon_search"];
leftView.backgroundColor = [UIColor clearColor];
_tf.leftView=leftView;
_tf.delegate = self;
//设置leftViewMode
_tf.leftViewMode=UITextFieldViewModeAlways;
//设置放大镜距离左边的间距,设置leftView的内容居中
leftView.contentMode=UIViewContentModeCenter;
//添加到导航栏中
self.navigationItem.titleView=_tf;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button addTarget:self action:@selector(cancelBtnClick:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(0, 0, 44, 44);
[button setTitle:@"取消" forState:UIControlStateNormal];
// 设置rightBarButtonItem
UIBarButtonItem *rightItem =[[UIBarButtonItem alloc] initWithCustomView:button];
self.navigationItem.rightBarButtonItem = rightItem;
_original = [[NSMutableArray alloc] initWithObjects:@"叶小丽", @"王小建",
@"王大才", @"高老师", @"江老师", @"刘博士", @"AI-Ghanim,MJ", @"zhao yuhui", nil];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
if (!cell)
{
//如果不存在,创建一个可重用cell
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:nil];
}
for(int i=0; i<_original.count; ++i){
//寻找关键词
if ([_original[i] rangeOfString:_search].location != NSNotFound) {
cell.textLabel.text = _original[i];
}
}
return cell;
}
-(void) cancelBtnClick:(UIButton *)btn{
[self.navigationController popViewControllerAnimated:NO];
}
-(void) searchMethod{
NSLog(@"search!!");
_tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, self.view.height)];
_tv.delegate = self;
_tv.dataSource = self;
[self.view addSubview:_tv];
if (_tf.text.length > 0)
{
// UITextField有输入
_search = _tf.text;
}
else{
// UITextField没有输入
_search = @"";
}
[self.tv reloadData];
}
@end