注:原文: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中显示
以上便是大致思路,看一遍代码基本上就理解了,还是不明白的欢迎留言
代码实现:
保存和读取历史记录
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> |
删除历史记录
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> |