[分享]IOS開發-簡單實現搜索框顯示歷史記錄的本地緩存及搜索歷史每次只能獲取到一個的解決方案


 

注:原文:http://www.zhimengzhe.com/IOSkaifa/40433.html

 

 

1.首先,我們需要對進行過搜索的textField的輸入內容進行一個NSUserDefaults的本地保存,由於我這里是利用的后台接口處理的具體關鍵字搜索,如果有做本地搜索的需要自行修改一下。那么我們就在搜索了之后(也就是點擊了“前往”那個按鈕之后,跳轉到下一個界面之前)進行保存即可。這樣做的目的有兩個:a.避免無效搜索占用本地保存的內存,也就是在textFiled中輸入了,但是沒有進行搜索,或者說節省了因為用戶的取消操作而占用的內存 b.執行邏輯:在搜索之后對搜索的內容關鍵字進行本地保存處理

2.我們需要利用一個全局的NSMutableArray來保存搜索的內容,每一次點擊鍵盤上的“搜索”時,都對這個NSMutableArray進行一次判斷:如果其有內容,就將其mutableCopy到我們相應方法中的這個局部的NSMutableArray中,這樣,我們之前所保存在這個全局的NSMutableArray中的數據就會添加到這個局部的NSMutableArray中,之后,我們將輸入的內容也添加進這個局部的NSMutableArray中,這樣就達到了不斷向NSMutableArray中添加數據的目的,而不是每一次都只能取得到一個內容

3.在-(void)viewWillAppear:(BOOL)animated中讀取歷史記錄並在tableView中顯示

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

代碼實現:

保存和讀取歷史記錄

?
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<code>-( 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];
}
 
</code>

刪除歷史記錄

?
code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<code> //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 @ "刪除" ;
}</code>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM