searchbar自动匹配搜索内容

本文介绍如何在iOS应用中实现UISearchBar的设置与监听功能,并通过UITableView展示搜索结果。文章详细展示了如何从本地文件读取数据进行筛选匹配,以及如何更新搜索结果视图。

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

-(viod) viewDidLoad{
    ///////////////////////////////////////////////////////////////////////////////////////////////// Add searchbar
    mySearchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0.0, 0.0, self.view.bounds.size.width, 40)];
    mySearchBar.placeholder=@"Please Enter";
    mySearchBar.delegate = self;
    mySearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
    mySearchBar.autocapitalizationType = UITextAutocapitalizationTypeNone;    
    [self.view addSubview:mySearchBar];
    [mySearchBar release];
    
    //////////////////////////////////////////////////////////////////////////////////////////////// Add search result table view

    searchTableView=[[UITableView alloc] initWithFrame:CGRectMake(0.0, 40.0, self.view.bounds.size.width, 160) style:UITableViewStylePlain];
    searchTableView.rowHeight=30;
    searchTableView.dataSource=self;
    searchTableView.delegate=self;
}
#pragma mark searchBar functions
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    searchArray=[[NSMutableArray alloc] init];

    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                     NSString *path=[paths objectAtIndex:0];
                     NSString *filename=[path stringByAppendingPathComponent:@"Info.plist"];//this file name which the info is saved
    
    NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
    NSInteger length=[searchText length];
    for (int i=0; i<[array count]; i++) {
        NSString* text=[array objectAtIndex:i];
        NSString* textTemp=[text substringToIndex:length];
        if ([textTemp isEqualToString:searchText]) {
            [searchArray addObject:text];
        }
    }
    [searchTableView reloadData];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [mySearchBar setShowsCancelButton:YES animated:YES];
    [self.view addSubview:searchTableView];//add TableView
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    mySearchBar.text=@"";
    [[self.view.subviews objectAtIndex:[self.view.subviews count]-1] removeFromSuperview];//remove the tableView if clicking the cancel button

    [mySearchBar setShowsCancelButton:NO animated:YES];
    [mySearchBar resignFirstResponder];
    
    searchArray=[[NSMutableArray alloc] init];
    [searchTableView reloadData];
}
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    [self searchBarPin];

    NSString* addressToData=[NSString stringWithFormat:@"%@",[[[resultArray objectForKey:@"results"] objectAtIndex:0] objectForKey:@"formatted_address"]];//把新输入的记录准备放入文件里
    [self writeIntoDataForAddress:[addressToData retain]];
}

-(void) writeIntoDataForAddress:(NSString*)address
{
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
                     NSString *path=[paths objectAtIndex:0];
                     NSString *filename=[path stringByAppendingPathComponent:@"Info.plist"];
    

    NSInteger function=0;
    NSMutableArray *array=[[NSMutableArray alloc] initWithContentsOfFile:filename];
    NSMutableArray *array2=[[NSMutableArray alloc]init];

                     //我试过想在提取的array里面直接addObject,然后写入文件,但没有成功,所以我重新写一个array,所以第一步就是要把原来的记录全部放入新的array里面,//所以有第一个循环
    for (int i=0; i<[array count]; i++) {
        [array2 addObject:[array objectAtIndex:i]];
    }
                    //下面这个循环比较新输入的记录在文件有没有这条记录
    for (int i=0; i<[array2 count]; i++) {
        if ([address isEqualToString:[array2 objectAtIndex:i]]) {
            function=1;//表示文件里面有这行数据,如果是0表示没有这行数据,需要添加
        }
    }
    if (function==0) {
        [array2  addObject:address];
    }
    
//    NSLog(@"%d",[array2 count]);
    [array2 writeToFile:filename  atomically:YES];
                     [array2 release];
}
#pragma mark tableView delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [searchArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle
                                       reuseIdentifier: CellIdentifier] autorelease];
    }
    
    // Configure the cell...
    NSUInteger row = [indexPath row];    
    cell.textLabel.text = [searchArray objectAtIndex:row];
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    mySearchBar.text=[[searchArray objectAtIndex:[indexPath row]] retain];
    [self searchBarPin];
}
-(void) searchBarPin{
//这里写当click search buton的时候需要干什么的coding
}
最后别忘了要建Info.plist这个文件来保存记录。

不好意思,因为我不在mac上,只能贴代码了。至于interface,你就按照提示的错误,在interface上添加吧。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值