iOS tableview实现简单搜索功能

时间:2022-08-30 20:48:42

本文实例为大家分享了tableview实现搜索功能的具体代码,供大家参考,具体内容如下

一、先用xcode创建好工程

iOS tableview实现简单搜索功能

通过xib文件来初始化视图控制器

iOS tableview实现简单搜索功能

二、编写代码

1、先为nsdictionary创建一个分类 实现字典的深拷贝

.h文件

?
1
2
3
4
5
6
#import <foundation/foundation.h>
 
@interface nsdictionary (mutabledeepcopy)
 
- (nsmutabledictionary *)mutabledeepcopy;
@end

.m文件

?
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
#import "nsdictionary+mutabledeepcopy.h"
 
@implementation nsdictionary (mutabledeepcopy)
 
- (nsmutabledictionary *)mutabledeepcopy
{
 nsmutabledictionary *mutabledictionary = [nsmutabledictionary dictionarywithcapacity:[self count]]; //这里的容量也只是个参考值,表示对大小的限制 大小是调用该方法的count
 nsarray *keys = [self allkeys]; //self就是个可变的字典
 for(id key in keys)
 {
  id dicvalue = [self valueforkey:key];
  //从 nsdictionary 取值的时候有两个方法objectforkey valueforkey
  id diccopy = nil;
  if([dicvalue respondstoselector:@selector(mutabledeepcopy)])
  //如果对象没有响应mutabledeepcopy 就创建一个可变副本 dicvalue 有没有实现这个方法
  {
   diccopy = [dicvalue mutabledeepcopy];
  }
  else if([dicvalue respondstoselector:@selector(mutablecopy)])
  {
   diccopy = [dicvalue mutablecopy];
  }
  if(diccopy ==nil)
  {
   diccopy = [dicvalue copy];
  }
  [mutabledictionary setvalue:diccopy forkey:key];
 }
 return mutabledictionary;
}
@end

2、编写主代码

.h文件
notescanviewcontroller.h

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#import <uikit/uikit.h>
 
@interface notescanviewcontroller : uiviewcontroller <uitableviewdatasource,uitableviewdelegate,uisearchbardelegate>
 
@property (nonatomic,retain)nsmutabledictionary *words;
@property (nonatomic,retain)nsmutablearray *keys;
@property (weak, nonatomic) iboutlet uitableview *table;
@property (weak, nonatomic) iboutlet uisearchbar *search;
 
@property (nonatomic,retain)nsdictionary *allwords;
- (void)resetsearch;
- (void)handlesearchforterm:(nsstring *)searchterm;
 
@end

.m文件

?
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#import "notescanviewcontroller.h"
#import "nsdictionary+mutabledeepcopy.h"
@interface notescanviewcontroller ()
 
@end
 
@implementation notescanviewcontroller
 
- (id)initwithnibname:(nsstring *)nibnameornil bundle:(nsbundle *)nibbundleornil
{
 self = [super initwithnibname:nibnameornil bundle:nibbundleornil];
 if (self) {
  // custom initialization
 }
 return self;
}
 
 
- (void)viewdidload //只在第一次加载视图调用
{
 [super viewdidload];
 /*加载plist文件*/
 nsstring *wordspath = [[nsbundle mainbundle]pathforresource:@"notesection" oftype:@"plist"];//得到属性列表的路径
 nsdictionary *dictionary = [[nsdictionary alloc]initwithcontentsoffile:wordspath];
 self.allwords = dictionary;
 [self resetsearch]; //加载并填充words可变字典和keys数组
 
 _search.autocapitalizationtype = uitextautocapitalizationtypenone;//不自动大写
 _search.autocorrectiontype = uitextautocorrectiontypeno;//不自动纠错
}
 
//取消搜索或者改变搜索条件
- (void)resetsearch
{
 self.words = [self.allwords mutabledeepcopy]; //得到所有字典的副本 得到一个字典
 nslog(@"所有字典 = %@",self.words);
 nsmutablearray *keyarray = [[nsmutablearray alloc]init];//创建一个可变数组
 [keyarray addobjectsfromarray:[[self.allwords allkeys]sortedarrayusingselector:@selector(compare:)]]; //用指定的selector对array的元素进行排序
 self.keys = keyarray; //将所有key 存到一个数组里面
 nslog(@"所有key = %@",self.keys);
}
//实现搜索方法
- (void)handlesearchforterm:(nsstring *)searchterm
{
 nsmutablearray *sectionsremove = [[nsmutablearray alloc]init]; //创建一个数组存放我们所找到的空分区
 [self resetsearch];
 for(nsstring *key in self.keys)//遍历所有的key
 {
  nsmutablearray *array = [_words valueforkey:key] ;  //得到当前键key的名称 数组
  nsmutablearray *toremove = [[nsmutablearray alloc]init];//需要从words中删除的值 数组
  for(nsstring *word in array) //实现搜索
  {
   if([word rangeofstring:searchterm options:nscaseinsensitivesearch].location == nsnotfound)//搜索时忽略大小写 把没有搜到的值 放到要删除的对象数组中去
    [toremove addobject:word]; //把没有搜到的内容放到 toremove中去
  }
  
  if([array count] == [toremove count])//校对要删除的名称数组长度和名称数组长度是否相等
   [sectionsremove addobject:key]; //相等 则整个分区组为空
  [array removeobjectsinarray:toremove]; //否则 删除数组中所有与数组toremove包含相同的元素
 }
 [self.keys removeobjectsinarray:sectionsremove];// 删除整个key 也就是删除空分区,释放用来存储分区的数组,并重新加载table 这样就实现了搜索
 [_table reloaddata];
}
 
- (void)viewwillappear:(bool)animated //当使用push或者prenset方式调用
{
}
//#pragma mark -
- (nsinteger)numberofsectionsintableview:(uitableview *)tableview
{
 return ([_keys count] >0)?[_keys count]:1; //搜索时可能会删除所有分区 则要保证要有一个分区
}
 
- (nsinteger)tableview:(uitableview *)tableview numberofrowsinsection:(nsinteger)section
{
 if([_keys count] == 0)
 {
  return 0;
 }
 nsstring *key = [_keys objectatindex:section]; //得到第几组的key
 nsarray *wordsection = [_words objectforkey:key]; //得到这个key里面所有的元素
 return [wordsection count]; //返回元素的个数
}
- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath
{
 nsuinteger section = [indexpath section]; //得到第几组
 nsuinteger row = [indexpath row]; //得到第几行
 nsstring *key = [_keys objectatindex:section]; //得到第几组的key
 nsarray *wordsection = [_words objectforkey:key]; //得到这个key里面的所有元素
 static nsstring *notesectionidentifier = @"notesectionidentifier";
 uitableviewcell *cell =[tableview dequeuereusablecellwithidentifier:notesectionidentifier];
 if(cell == nil)
 {
  cell = [[uitableviewcell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:notesectionidentifier];
  
 }
 cell.textlabel.text = [wordsection objectatindex:row];
 return cell;
}
//为每个分区指定一个标题
- (nsstring *)tableview:(uitableview *)tableview titleforheaderinsection:(nsinteger)section
{
 if([_keys count] == 0)
  return @" ";
 nsstring *key = [_keys objectatindex:section];
 return key;
}
//创建一个索引表
- (nsarray *)sectionindextitlesfortableview:(uitableview *)tableview
{
 return _keys;
}
#pragma mark -
 
- (nsindexpath *)tableview:(uitableview *)tableview willselectrowatindexpath:(nsindexpath *)indexpath
{
 [_search resignfirstresponder]; //点击任意 cell都会取消键盘
 return indexpath;
}
 
#pragma mark-
 
- (void)searchbarsearchbuttonclicked:(uisearchbar *)searchbar //搜索button点击事件
{
 nsstring *searchterm = [searchbar text];
 [self handlesearchforterm:searchterm]; //搜索内容 删除words里面的空分区和不匹配内容
}
 
- (void)searchbar:(uisearchbar *)searchbar textdidchange:(nsstring *)searchtext
{ //搜索内容随着输入及时地显示出来
 if([searchtext length] == 0)
 {
  [self resetsearch];
  [_table reloaddata];
  return;
 }
 else
  [self handlesearchforterm:searchtext];
}
 
- (void)searchbarcancelbuttonclicked:(uisearchbar *)searchbar //点击取消按钮
{
 _search.text = @""; //标题 为空
 [self resetsearch]; //重新 加载分类数据
 [_table reloaddata];
 [searchbar resignfirstresponder]; //退出键盘
 
}
@end

运行结果

iOS tableview实现简单搜索功能iOS tableview实现简单搜索功能

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:http://blog.csdn.net/itpeng523/article/details/23035679