将MainTableViewController作为导航控制器的根控制器
_window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
_window.backgroundColor = [UIColor whiteColor];
[_window makeKeyAndVisible];
MainTableViewController *mainViewCtrl = [[MainTableViewController alloc] initWithStyle:UITableViewStylePlain];
UINavigationController *navCtrl = [[UINavigationController alloc] initWithRootViewController:mainViewCtrl];
_window.rootViewController = navCtrl;
#import <UIKit/UIKit.h>
@interface MainTableViewController : UITableViewController<UITableViewDelegate>{
UITableView *_tableView;//表视图
NSArray *array;
NSMutableArray *_date;
}
#import "MainTableViewController.h"
@interface MainTableViewController ()
@end
@implementation MainTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
array = [UIFont familyNames];
_date = [NSMutableArray arrayWithArray:array];
//设置导航栏
self.navigationController.navigationBar.barStyle = UIBarStyleBlackOpaque;
//创建输入框视图
UITextField *textfield = [[UITextField alloc] initWithFrame:CGRectMake(85, 0, 190, 30)];
textfield.backgroundColor = [UIColor whiteColor];
//输入框的圆角
textfield.layer.cornerRadius = 5;
[textfield addTarget:self action:@selector(textfield:) forControlEvents:UIControlEventEditingChanged];
[self.navigationController.navigationBar addSubview:textfield];
#pragma mark - Table view data source
//设置单元格
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return _date.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *iden = @"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden ];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden] autorelease];
}
cell.textLabel.text = _date[indexPath.row];
return cell;
}
- (void)textfield:(UITextField *)textfield{
NSString *text = textfield.text;
if ([text length] == 0) {
_date = [[NSMutableArray arrayWithArray:array]retain];
[self.tableView reloadData];
return;
}
// 过滤条件
NSString *p = [NSString stringWithFormat:@"SELF LIKE[c] '%@*'",text];
// 谓词过滤
NSPredicate *predicate = [NSPredicate predicateWithFormat:p];
NSArray *arr = [array filteredArrayUsingPredicate:predicate];
_date = [[NSMutableArray arrayWithArray:arr]retain];
//刷新表视图
[self.tableView reloadData];
}