iOS - 搜索历史记录(本地归档实现)

2018-08-09  本文已影响981人  CDLOG

主要功能:历史记录的去重,新搜索的排第一,清除历史记录

1,首先用两个数组来保存搜索记录(为了新搜索的文字排第一而不会出错)
一个用来归档,保证归档的顺序是正序的(新搜的归档到最后)
一个用来解档,倒序显示归档的内容(新搜的排第一)
归档核心代码:

//一个用来归档,一个用来显示
@property (strong,nonatomic) NSMutableArray <HistorySearchModel *>* historySearchArr;
@property (strong,nonatomic) NSMutableArray <HistorySearchModel *>* historyShowSearchArr;

//搜索
-(void)search{
   if ([_inputUITextField.text isValid]) {
       NSLog(@"搜索了%@",_inputUITextField.text);
       self.searchLable.text = _inputUITextField.text;
//给归档的数组添加一个模型
       [self addHistoryModelWithText:_inputUITextField.text andType:HistorySearchSuplly];
//归档需要归档的数组
       [self saveHistorySearch];
   }
}
//判断搜索记录是否重复后添加到归档数组
-(void)addHistoryModelWithText:(NSString *)text andType:(HistorySearchType)type{
   //    重复的标志
   NSArray * array = [NSArray arrayWithArray: self.historySearchArr];
   BOOL isRepet = NO;
   for (HistorySearchModel *model in array) {
       if (model.type == HistorySearchSuplly &&  [model.title isEqualToString:text]) {
           [self.historySearchArr removeObject:model];
           [self.historySearchArr addObject:[HistorySearchModel initWithTitle:text andType:type]];
           isRepet = YES;
       }
   }
   if (!isRepet) {
       [self.historySearchArr addObject:[HistorySearchModel initWithTitle:text andType:type]];
   }
}
//归档方法
-(void)saveHistorySearch{
   NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
   NSString *filePath = [Path stringByAppendingPathComponent:@"historySearch.data"]; //注:保存文件的扩展名可以任意取,不影响。
   //归档
   [NSKeyedArchiver archiveRootObject:self.historySearchArr toFile:filePath];
}

解档显示核心代码:

//刷新搜索记录
- (IBAction)reloadSearch:(id)sender {
    //先移除原来的数据
    [self.historyShowSearchArr removeAllObjects];
    //解档加载新数据
    [self readHistorySearch];
    if (self.searchCellHeight * self.historyShowSearchArr.count+250>self.BaseTableView.bottom-50) {
        self.searchTableViewHeight.constant = self.BaseTableView.height-110;
    }else{
        self.searchTableViewHeight.constant = self.searchCellHeight * self.historyShowSearchArr.count;
    }
    [self.historySearchTableVIew reloadData];
    
}
    //解档加载新数据
//历史搜索解档
-(void)readHistorySearch{
    NSString *Path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *filePath = [Path stringByAppendingPathComponent:@"historySearch.data"];
    //解档
    NSMutableArray<HistorySearchModel *> *personArr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
//       self.historySearchArr =(NSMutableArray *) [[self.historySearchArr     reverseObjectEnumerator]allObjects];
    self.historySearchArr = [NSMutableArray arrayWithArray:personArr];
    self.historyShowSearchArr =[NSMutableArray arrayWithArray:(NSMutableArray *) [[self.historySearchArr reverseObjectEnumerator]allObjects]];
   [self.historySearchTableVIew reloadData];
}

清除历史记录核心代码

[self.historySearchArr removeAllObjects];
[self.historyShowSearchArr removeAllObjects];
[self saveHistorySearch];
[self.historySearchTableVIew reloadData];

GitHub:https://github.com/CDLOG/histotySearch

上一篇下一篇

猜你喜欢

热点阅读