根据首字母查找单词

来源:互联网 发布:校验md5码软件 编辑:程序博客网 时间:2024/05/16 00:28






将MainTableViewController作为导航控制器的根控制器


 _window = [[UIWindowalloc] initWithFrame:[UIScreenmainScreen].bounds];

    _window.backgroundColor = [UIColorwhiteColor];

    [_windowmakeKeyAndVisible];

    MainTableViewController *mainViewCtrl = [[MainTableViewControlleralloc] initWithStyle:UITableViewStylePlain];

    

    UINavigationController *navCtrl = [[UINavigationControlleralloc] 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 {

    [superviewDidLoad];

    

    

    

    array = [UIFontfamilyNames];

    

    _date = [NSMutableArrayarrayWithArray:array];

    

    //设置导航栏

    self.navigationController.navigationBar.barStyle =UIBarStyleBlackOpaque;


//创建输入框视图

   UITextField *textfield = [[UITextFieldalloc] initWithFrame:CGRectMake(85,0, 190, 30)];

    

    

    textfield.backgroundColor = [UIColorwhiteColor];

    


   //输入框的圆角

textfield.layer.cornerRadius =5;

   

    [textfield addTarget:selfaction:@selector(textfield:)forControlEvents:UIControlEventEditingChanged];

    

    [self.navigationController.navigationBaraddSubview: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 = [tableViewdequeueReusableCellWithIdentifier:iden ];

    

    

   if (cell == nil) {

        cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:iden] autorelease];

        

        

    }

    

    

    cell.textLabel.text =_date[indexPath.row];

    

   return cell;

}



- (void)textfield:(UITextField *)textfield{

    

   NSString *text = textfield.text;

       if ([text length] ==0) {

           _date = [[NSMutableArrayarrayWithArray:array]retain];

            

    

            [self.tableViewreloadData];

    

           return;

    

        }

    //    过滤条件

       NSString *p = [NSStringstringWithFormat:@"SELF LIKE[c] '%@*'",text];

    

    //    谓词过滤

       NSPredicate *predicate = [NSPredicatepredicateWithFormat:p];

    

       NSArray *arr = [arrayfilteredArrayUsingPredicate:predicate];

    _date = [[NSMutableArrayarrayWithArray:arr]retain];

        

//刷新表视图

        [self.tableViewreloadData];

        


    

    

}







    














0 0