UISearchBar和UITableView实现简单的模糊查询

在UISearchBar的代理方法中有两个方法可以比较好的实现:

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText;//方法一

- (BOOL)searchBar:(UISearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;//方法二

但是方法2有一个缺点,就是只有在点击键盘上的按钮时才会被调用,而点击汉字选择栏里面的汉字没有反应,方法一就不存在这个问题,所以我们这次选择方法一

下面直接通过代码实现来观察

1.首先遵循协议
<UISearchBarDelegate,UITableViewDataSource,UITableViewDelegate>
2.定义属性
@property (strong,nonatomic) UITableView *myTableView;
@property (strong,nonatomic) NSMutableArray *displayArray;//展示的数据
@property (strong,nonatomic) NSArray *oldDataArray;//存放原始数据
@property (strong,nonatomic) UISearchBar *mySearchBar;
3.在- (void)viewDidLoad添加控件和添加原始数据以及展示数据
- (void)viewDidLoad
{
    [super viewDidLoad];

    self.oldDataArray=@[@"大中国的中国人",@"一本大书",@"五子棋",@"百年中国在东方",@"猴子上树偷桃子",@"老虎上山吃鸡",@"小马过河没淹死",@"今年回家过年",@"小南回来了",@"我还是我",@"不一样的烟火",@"今天下雪了",@"下雪了路很滑",@"国家图书馆在北京",@"在上班",@"请不要打扰我",@"你叫什么名字",@"保尔咔嚓金",@"很不错的天气",@"大妈在路上",@"我要过马路",@"今天星期几",@"小孩都上学",@"林黛玉与薛宝钗"];
    self.displayArray=[[NSMutableArray alloc]initWithArray:self.oldDataArray];
    
    self.mySearchBar=[[UISearchBar alloc]initWithFrame:CGRectMake(0, 20, 320, 50)];
    self.mySearchBar.delegate=self;
    [self.view addSubview:self.mySearchBar];
    
    self.myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 70, 320, self.view.frame.size.height-70)style:UITableViewStyleGrouped];
    self.myTableView.dataSource = self;
    self.myTableView.delegate = self;
    self.myTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
    [self.view addSubview:self.myTableView];
}
4.实现UITableView上数据的现实(实现UITableViewDataSourceUITableViewDelegate的方法)
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.displayArray.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}
-(UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *Identifier = @"Mycell";
    UITableViewCell * cell = [tableView dequeueReusableHeaderFooterViewWithIdentifier:Identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (self.displayArray.count == 0)
    {
        return cell;
    }

    cell.textLabel.text=self.displayArray[indexPath.row];
    
    return cell;
}
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"您选中的为:%@",self.displayArray[indexPath.row]);
}
5.实现方法一和模糊查询功能
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"查询的参数====%@",searchText);
    [self searchFromTalbeView:searchText];
}

- (void)searchFromTalbeView:(NSString *)text
{
    NSString *searchText=text;
    if (searchText.length>0) {
        [self.displayArray removeAllObjects];
        for (NSString *tempStr in self.oldDataArray)
        {
            NSRange titleResult=[tempStr rangeOfString:searchText options:NSCaseInsensitiveSearch];
            if (titleResult.length>0)
            {
                [self.displayArray addObject:tempStr];
            }
        }
    }
    else
    {
        self.displayArray=[self.oldDataArray mutableCopy];
    }
    [self.myTableView reloadData];
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值