[分享]iOS开发-简单实现搜索框显示历史记录的本地缓存及搜索历史每次只能获取到一个的解决方案

我们先来看一下运行后的效果,大致就是这样,具体优化可以根据自己的项目需要来更改

《[分享]iOS开发-简单实现搜索框显示历史记录的本地缓存及搜索历史每次只能获取到一个的解决方案》

下面来说一下思路:
1.首先,我们需要对进行过搜索的textField的输入内容进行一个NSUserDefaults的本地保存,由于我这里是利用的后台接口处理的具体关键字搜索,如果有做本地搜索的需要自行修改一下。那么我们就在搜索了之后(也就是点击了“前往”那个按钮之后,跳转到下一个界面之前)进行保存即可。这样做的目的有两个:a.避免无效搜索占用本地保存的内存,也就是在textFiled中输入了,但是没有进行搜索,或者说节省了因为用户的取消操作而占用的内存 b.执行逻辑:在搜索之后对搜索的内容关键字进行本地保存处理

2.我们需要利用一个全局的NSMutableArray来保存搜索的内容,每一次点击键盘上的“搜索”时,都对这个NSMutableArray进行一次判断:如果其有内容,就将其mutableCopy到我们相应方法中的这个局部的NSMutableArray中,这样,我们之前所保存在这个全局的NSMutableArray中的数据就会添加到这个局部的NSMutableArray中,之后,我们将输入的内容也添加进这个局部的NSMutableArray中,这样就达到了不断向NSMutableArray中添加数据的目的,而不是每一次都只能取得到一个内容

3.在-(void)viewWillAppear:(BOOL)animated中读取历史记录并在tableView中显示

以上便是大致思路,看一遍代码基本上就理解了,还是不明白的欢迎留言

代码实现:

保存和读取历史记录

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    NSString * searchHistory = [MyUtil getObjectForKey:@"searchHistory"];
//    if (searchHistory) {
//        [self.historyArray addObject:searchHistory];
//        [self.tableView reloadData];
//    }
    [self readNSUserDefaults];
}
 
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    DLog(@"开始搜索");
    self.tableView.hidden = NO;
}
 
 
-(IBAction)go:(UITextField *)sender {
    DLog(@"点击go");
    if (self.textField.text.length == 0) {
        [MyUtil showTipText:@"搜索内容不能为空"];
        return;
    }
//        [MyUtil saveObject:self.textField.text forKey:@"searchHistory"];
        [self SearchText:self.textField.text];
    
        GYSearchDetailedViewController * searchDetailed = getViewController(@"searchDetailed", @"FindDoctor");
        searchDetailed.searchInfo = self.textField.text;
        [self.navigationController pushViewController:searchDetailed animated:YES];
   
    
}
 
-(void)SearchText:(NSString *)seaTxt
{
//    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
//    //读取数组NSArray类型的数据
//    self.historyArray = [userDefaultes arrayForKey:@"searchHistory"];
    
//    NSMutableArray *searTXT = [self.historyArray mutableCopy];
    NSMutableArray *searTXT = [[NSMutableArray alloc] init];
    if (self.historyArray) {
        searTXT = [self.historyArray mutableCopy];
    }
    [searTXT addObject:seaTxt];
    
    //将上述数据全部存储到NSUserDefaults中
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:searTXT forKey:@"searchHistory"];
}
 
-(void)readNSUserDefaults
{
    NSUserDefaults *userDefaultes = [NSUserDefaults standardUserDefaults];
    //读取数组NSArray类型的数据
//    NSArray *myArray = [userDefaultes arrayForKey:@"searchHistory"];
//    NSLog(@"myArray======%@",myArray);
    self.historyArray = [userDefaultes arrayForKey:@"searchHistory"];
    [self.tableView reloadData];
}

删除历史记录

//cell允许编辑
-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}
 
//删除历史记录
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        [self.historyArray removeObjectAtIndex:indexPath.row - 1];
 
        [self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationFade];
        
    }
}
 
//修改编辑按钮文字
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return @"删除";
}
    原文作者:ShevaKuilin
    原文地址: https://segmentfault.com/a/1190000005052542
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞