根据首字母查找单词

该博客介绍了一个基于UITableView的应用,实现了通过输入框实时根据首字母过滤并显示字体名称的功能。在MainTableViewController中,通过监听UITextField的编辑变化,利用谓词过滤数据,并更新UITableView的数据源,从而实现动态搜索效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >






将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];

        


    

    

}







    














评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值