本文實例為大家分享了tableview實現搜索功能的具體代碼,供大家參考,具體內容如下
一、先用xcode創建好工程
通過xib文件來初始化視圖控制器
二、編寫代碼
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 |
運行結果
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:http://blog.csdn.net/itpeng523/article/details/23035679