一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - IOS - iOS tableview實現簡單搜索功能

iOS tableview實現簡單搜索功能

2021-04-06 15:34xunxun523 IOS

這篇文章主要為大家詳細介紹了iOS tableview實現簡單搜索功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了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

延伸 · 閱讀

精彩推薦
  • IOSiOS開發之視圖切換

    iOS開發之視圖切換

    在iOS開發中視圖的切換是很頻繁的,獨立的視圖應用在實際開發過程中并不常見,除非你的應用足夠簡單。在iOS開發中常用的視圖切換有三種,今天我們將...

    執著丶執念5282021-01-16
  • IOSiOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果

    這篇文章主要介紹了iOS自定義UICollectionViewFlowLayout實現圖片瀏覽效果的相關資料,需要的朋友可以參考下...

    jiangamh8882021-01-11
  • IOSiOS中UILabel實現長按復制功能實例代碼

    iOS中UILabel實現長按復制功能實例代碼

    在iOS開發過程中,有時候會用到UILabel展示的內容,那么就設計到點擊UILabel復制它上面展示的內容的功能,也就是Label長按復制功能,下面這篇文章主要給大...

    devilx12792021-04-02
  • IOSiOS實現控制屏幕常亮不變暗的方法示例

    iOS實現控制屏幕常亮不變暗的方法示例

    最近在工作中遇到了要將iOS屏幕保持常亮的需求,所以下面這篇文章主要給大家介紹了關于利用iOS如何實現控制屏幕常亮不變暗的方法,文中給出了詳細的...

    隨風13332021-04-02
  • IOSiOS中MD5加密算法的介紹和使用

    iOS中MD5加密算法的介紹和使用

    MD5加密是最常用的加密方法之一,是從一段字符串中通過相應特征生成一段32位的數字字母混合碼。對輸入信息生成唯一的128位散列值(32個字符)。這篇文...

    LYSNote5432021-02-04
  • IOS詳解iOS中多個網絡請求的同步問題總結

    詳解iOS中多個網絡請求的同步問題總結

    這篇文章主要介紹了詳解iOS中多個網絡請求的同步問題總結,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧...

    liang199111312021-03-15
  • IOSiOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)

    iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和

    這篇文章主要介紹了iOS中滑動控制屏幕亮度和系統音量(附加AVAudioPlayer基本用法和Masonry簡單使用)的相關資料,需要的朋友可以參考下...

    CodingFire13652021-02-26
  • IOSiOS開發技巧之狀態欄字體顏色的設置方法

    iOS開發技巧之狀態欄字體顏色的設置方法

    有時候我們需要根據不同的背景修改狀態欄字體的顏色,下面這篇文章主要給大家介紹了關于iOS開發技巧之狀態欄字體顏色的設置方法,文中通過示例代碼...

    夢想家-mxj8922021-05-10
主站蜘蛛池模板: 扒开女人下面使劲桶屁股动漫 | 精品国产美女福利在线 | 亚洲国产韩国欧美在线不卡 | 青青青青久久国产片免费精品 | 欧美vpswindows| 日产一区二区 | 性欧美金发洋妞xxxxbbbb | 国内自拍网红在线综合 | 日本精品中文字幕在线播放 | 日本国产成人精品视频 | 国产一区二区三区四 | 四虎网站入口 | 国产区1 | 亚洲成在人网站天堂一区二区 | 美女一级ba大片免色 | 亚洲成人免费观看 | 日韩欧美一区黑人vs日本人 | 久青草国产在线观看视频 | 四虎免费在线观看视频 | 久久综合香蕉久久久久久久 | 午夜片神马影院福利 | 无套日出白浆在线播放 | 国产成人精品一区二三区在线观看 | 久久亚洲精品中文字幕60分钟 | 国产香蕉一区二区精品视频 | 日本暖暖在线 | 欧美a欧美1级| 探花 在线| 日韩高清在线免费观看 | 欧美日韩国产另类一区二区三区 | 极品美女穴 | 日本免费一区二区三区四区五六区 | 女人用粗大自熨喷水在线视频 | 欧美一级精品 | 我在厨房摸岳的乳HD在线观看 | 精品无人区麻豆乱码无限制 | 91精品国产综合久久消防器材 | 欧洲vodafonewi喷潮 | 黑人巨荃大战乌克兰美女 | 被强迫调教的高辣小说 | 校园春色偷拍自拍 |