设置带有导航栏的根视图控制器
RootViewController.h
@interface RootViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,UITextFieldDelegate> { UITableView *_tableView; } @property(nonatomic, retain)NSArray *data; //存放原本的数据 @property(nonatomic, retain)NSArray *filterData; //用于存放删选后的数据
- (void)viewDidLoad { [super viewDidLoad]; //创建输入框 UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 200, 30)]; textField.delegate = self; textField.borderStyle = UITextBorderStyleRoundedRect; //关闭大写 textField.autocapitalizationType = UITextAutocapitalizationTypeNone; self.navigationItem.titleView = textField; [textField release]; //创建表视图 _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 480) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; //取得数据 /* [@"1",@"2"] */ _data = [[UIFont familyNames] retain]; _filterData = [_data retain]; //注册一个通知 //输入框里面的内容发生改变的时候发送通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeAction:) name:UITextFieldTextDidChangeNotification object:nil]; } - (void)changeAction:(NSNotification *)notificiation { //输入框输入的内容 NSLog(@"%@",notificiation.object); UITextField *textField = notificiation.object; //定义谓词 //[c]标示不区分大小写 NSString *t = [NSString stringWithFormat:@"self like [c]'*%@*'",textField.text]; NSPredicate *predicate = [NSPredicate predicateWithFormat:t]; //过滤元素 self.filterData = [_data filteredArrayUsingPredicate:predicate]; //刷新视图 [_tableView reloadData]; } #pragma mark - UITableView dataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return _filterData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *iden = @"cell110"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease]; } cell.textLabel.text = _filterData[indexPath.row]; return cell; } #pragma mark - UITextField delegate //- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string { // // /* // textField.text里面存放是用户以前输入的内容 // string:标示用户刚刚输入的字符 // */ // // NSLog(@"textField:%@",textField.text); // NSLog(@"string:%@",string); // // //输入框输入的内容 // NSString *text = [NSString stringWithFormat:@"%@%@",textField.text,string]; // // //定义谓词 // //[c]标示不区分大小写 // NSString *t = [NSString stringWithFormat:@"self like [c]'*%@*'",text]; // NSPredicate *predicate = [NSPredicate predicateWithFormat:t]; // // //过滤元素 // self.filterData = [_data filteredArrayUsingPredicate:predicate]; // // //刷新视图 // [_tableView reloadData]; // // // return YES; //} - (BOOL)textFieldShouldReturn:(UITextField *)textField { //收起键盘 [textField resignFirstResponder]; return YES; }
原文地址:http://blog.csdn.net/pengyuan_d/article/details/40615875