本文主要讲述了如何使用UISearchController实现搜索栏,现在把实现思路和代码整理出来分享给iOS程序员兄弟们,希望给他们的开发工作带来帮助。
1 |
UISearchController 继承自UIViewController // 在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISearchDisplayController的组合方式.
1 | <span style= "font-size: medium;" ># import SearchViewController.h |
2 | #define kTableCell @tablecell |
3 | @interface SearchViewController () |
4 | @property (nonatomic,retain)NSMutableArray *items; |
5 | @property (nonatomic,retain)UISearchController *searchController; |
6 | @property (nonatomic,retain)NSMutableArray *searchResults; //接收数据源结果 |
7 | @end </span> |
懒加载及释放
1 | <span style= "font-size: medium;" > @implementation SearchViewController |
2 |
3 | - (instancetype)init |
4 | { |
5 | self = [ super init]; |
6 | if (self) { |
7 | } |
8 | return self; |
9 | } |
10 | //懒加载 |
11 | - (NSMutableArray *)items{ |
12 | if (!_items) { |
13 | self.items = [NSMutableArray arrayWithCapacity: 0 ]; |
14 | } |
15 | return [[_items retain]autorelease]; |
16 | } |
17 | //懒加载 |
18 | - (NSMutableArray *)searchResults{ |
19 | if (!_searchResults) { |
20 | self.searchResults = [NSMutableArray arrayWithCapacity: 0 ]; |
21 | } |
22 | return [[_searchResults retain]autorelease]; |
23 | } |
24 | //释放 |
25 | - ( void )dealloc |
26 | { |
27 | self.items = nil; |
28 | self.searchResults = nil; |
29 | self.searchController = nil; |
30 | [ super dealloc]; |
31 | }</span> |
调用:
1 | <span style= "font-size: medium;" >- ( void )viewDidLoad { |
2 | [ super viewDidLoad]; |
3 | //导航栏 |
4 | [self p_navigationController]; |
5 |
6 | //调用初始化searchController |
7 | [self setSearchControllerView]; |
8 |
9 | //注册 |
10 | [self.tableView registerClass:[UITableViewCell class ] forCellReuseIdentifier:kTableCell]; |
11 |
12 | }</span> |
初始化SearchController初始化
1 | <span style= "font-size: medium;" >- ( void )setSearchControllerView{ |
2 | self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil]; |
3 | self.searchController.searchBar.frame = CGRectMake( 0 , 0 , 0 , 44 ); |
4 | self.searchController.dimsBackgroundDuringPresentation = false ; |
5 | //搜索栏表头视图 |
6 | self.tableView.tableHeaderView = self.searchController.searchBar; |
7 | [self.searchController.searchBar sizeToFit]; |
8 | //背景颜色 |
9 | self.searchController.searchBar.backgroundColor = [UIColor orangeColor]; |
10 | self.searchController.searchResultsUpdater = self; |
11 |
12 | }</span> |
//引入UISearchController之后, UITableView的内容也要做相应地变动: 即cell中要呈现的内容是items, 还是searchResults.这一点, 可以通过UISearchController的active属性来判断, 即判断输入框是否处于active状态.
//UITableView相关的很多方法都要根据active来做判断:
配置导航条
1 | <span style= "font-size: medium;" >- ( void )p_navigationController{ |
2 | self.title = @小韩哥-英雄列表; |
3 | self.items = @[@国服第一臭豆腐 No. 1 Stinky Tofu CN., |
4 | @比尔吉沃特(Bill Ji walter), |
5 | @瓦洛兰 Valoran, |
6 | @祖安 Zaun, |
7 | @德玛西亚 Demacia, |
8 | @诺克萨斯 Noxus, |
9 | @艾欧尼亚 Ionia, |
10 | @皮尔特沃夫 Piltover, |
11 | @弗雷尔卓德 Freijord, |
12 | @班德尔城 Bandle City, |
13 | @无畏先锋, |
14 | @战争学院 The Institute of War, |
15 | @巨神峰, |
16 | @雷瑟守备(JustThunder), |
17 | @裁决之地(JustRule), |
18 | @黑色玫瑰(Black Rose), |
19 | @暗影岛(Shadow island), |
20 | @钢铁烈阳(Steel fierce), |
21 | @恕瑞玛沙漠 Shurima Desert, |
22 | @均衡教派(Balanced sect), |
23 | @水晶之痕(Crystal Scar), |
24 | @影流(Shadow Flow ), |
25 | @守望之海(The Watchtower of sea), |
26 | @皮尔特沃夫, |
27 | @征服之海, |
28 | @扭曲丛林 Twisted Treeline, |
29 | @教育网专区, |
30 | @试炼之地 Proving Grounds, |
31 | @卡拉曼达 Kalamanda, |
32 | @蓝焰岛 Blue Flame Island, |
33 | @哀嚎沼泽 Howling Marsh, |
34 | @艾卡西亚 Icathia, |
35 | @铁脊山脉 Ironspike Mountains, |
36 | @库莽古丛林 Kumungu, |
37 | @洛克法 Lokfar, |
38 | @摩根小道 Morgon Pass, |
39 | @塔尔贡山脉 Mountain Targon, |
40 | @瘟疫丛林 Plague Jungles, |
41 | @盘蛇河 Serpentine River, |
42 | @厄尔提斯坦 Urtistan, |
43 | @巫毒之地 Voodoo Lands, |
44 | @咆哮深渊 Howling Abyss, |
45 | @熔岩洞窟 Magma Chambers, |
46 | @召唤师峡谷 Summoner's Rift, |
47 | @九尾妖狐: 阿狸(Ahri), |
48 | @暗影之拳:阿卡丽(Akali), |
49 | @牛头酋长:阿利斯塔(Alistar), |
50 | @殇之木乃伊:阿木木(Amumu), |
51 | @冰晶凤凰:艾尼维亚(Anivia), |
52 | @黑暗之女:安妮(Annie), |
53 | @寒冰射手:艾希(Ashe), |
54 | @蒸汽机器人:布里茨(Blitzcrank), |
55 | @复仇焰魂:布兰德(Brand), |
56 | @皮城女警:凯特琳(Caitlyn), |
57 | @魔蛇之拥:卡西奥佩娅(Cassiopeia), |
58 | @虚空恐惧:科’加斯(ChoGath), |
59 | @英勇投弹手:库奇(Corki), |
60 | @诺克萨斯之手:德莱厄斯(Darius), |
61 | @皎月女神:黛安娜:(Diana), |
62 | @祖安狂人:蒙多医生(DrMundo), |
63 | @荣耀行刑官:德莱文(Delevin), |
64 | @蜘蛛女皇:伊莉斯(Elise), |
65 | @寡妇制造者:伊芙琳(Evelynn), |
66 | @探险家:伊泽瑞尔(Ezreal), |
67 | @末日使者:费德提克(Fiddlesticks), |
68 | @无双剑姬:剑姬(Fiora), |
69 | @潮汐海灵:菲兹(Fizz), |
70 | @哨兵之殇:加里奥(Galio), |
71 | @海洋之灾:普朗克(Gangplank), |
72 | @德玛西亚之力:盖伦(Garen), |
73 | @酒桶:古拉加斯(Gragas), |
74 | @法外狂徒:格雷福斯(Graves), |
75 | @战争之影:赫卡里姆 (Hecarim), |
76 | @大发明家:黑默丁格(Heimerdinger), |
77 | @刀锋意志:伊瑞利亚(Irelia), |
78 | @风暴之怒:迦娜(Janna), |
79 | @德玛西亚皇子:嘉文四世(JarvanⅣ), |
80 | @武器大师:贾克斯(Jax), |
81 | @未来守护者:杰斯(Jayce), |
82 | @天启者:卡尔玛(Karma), |
83 | @死亡颂唱者:卡尔萨斯(Karthus), |
84 | @虚空行者:卡萨丁(Kassadin), |
85 | @不详之刃:卡特琳娜(Katarina), |
86 | @审判天使:凯尔(Kayle), |
87 | @狂暴之心:凯南(Kennen), |
88 | @虚空掠夺者:卡’兹克(Khazix), |
89 | @深渊巨口:克格’莫(Kog Maw), |
90 | @诡术妖姬:乐芙兰(LeBlanc), |
91 | @盲僧:李青(Lee sin), |
92 | @曙光女神:蕾欧娜(Leona), |
93 | @仙灵女巫:璐璐(lulu), |
94 | @光辉女郎:拉克丝(Lux), |
95 | @熔岩巨兽:墨菲特(Malphite), |
96 | @虚空先知:玛尔扎哈(Malzahar), |
97 | @扭曲树精:茂凯(Maokai), |
98 | @无极剑圣:易(Yi), |
99 | @赏金猎人:厄运小姐(MissFortune), |
100 | @齐天大圣:孙悟空(Monkey king), |
101 | @金属大师:莫德凯撒(Mordekaiser), |
102 | @堕天使:莫甘娜(Morgana), |
103 | @唤潮鲛姬:娜美(Nami), |
104 | @沙漠死神:内瑟斯(Nasus), |
105 | @深海泰坦:诺提勒斯(Nautilus), |
106 | @狂野女猎手:奈德丽(Nidalee), |
107 | @永恒梦魇:魔腾(Nocturne), |
108 | @雪人骑士:努努(Nunu), |
109 | @狂战士:奥拉夫(Olaf), |
110 | @发条魔灵:奥莉安娜(Orianna), |
111 | @战争之王:潘森(Pantheon), |
112 | @钢铁大使:波比(Poopy), |
113 | @披甲龙龟:拉莫斯(Rammus), |
114 | @荒漠屠夫:雷克顿(Renekton), |
115 | @傲之追猎者:雷恩加尔(Rengar), |
116 | @放逐之刃:瑞文(Rivan), |
117 | @机械公敌:兰博(Rumble), |
118 | @流浪法师:瑞兹(Ryze), |
119 | @凛冬之怒:瑟庄妮:(Sejuani), |
120 | @恶魔小丑:萨科(Shaco), |
121 | @暮光之眼:慎(Shen), |
122 | @龙血武姬:希瓦娜(Shyvana), |
123 | @炼金术士:辛吉德(Singed), |
124 | @亡灵勇士:赛恩(Sion), |
125 | @战争女神:希维尔(Sivir), |
126 | @水晶先锋:斯卡纳(Skarner), |
127 | @琴瑟仙女:娑娜(Sona), |
128 | @众星之子:索拉卡(Soraka), |
129 | @策士统领:斯维因(Swain), |
130 | @暗黑元首:辛德拉(Syndra), |
131 | @刀锋之影:泰隆(Talon), |
132 | @宝石骑士:塔里克(Taric), |
133 | @迅捷斥候:提莫(Teemo), |
134 | @魂锁典狱长:锤石(Thresh), |
135 | @麦林炮手:崔丝塔娜(Tristana), |
136 | @诅咒巨魔:特兰德尔(Trundle), |
137 | @蛮族之王:泰达米尔(Tryndamere), |
138 | @卡牌大师:崔斯特(Twisted Fate), |
139 | @瘟疫之源:图奇(Twitch), |
140 | @野兽之灵:乌迪尔(Udyr), |
141 | @首领之傲:厄加特(Urgot), |
142 | @惩戒之箭:韦鲁斯(Varus), |
143 | @暗夜猎手:薇恩(Vayne), |
144 | @邪恶小法师:维伽(Veigar), |
145 | @皮城执法官:蔚(Vi), |
146 | @机械先驱:维克托(Viktor), |
147 | @猩红收割者:弗拉基米尔(Vladimir), |
148 | @雷霆咆哮:沃利贝尔(Volibear), |
149 | @嗜血猎手:沃里克(Warwick), |
150 | @远古巫灵:泽拉斯(Xerath), |
151 | @德邦总管:赵信(XinZhao), |
152 | @掘墓者:约里克(Yorick), |
153 | @影流之主:劫(Zed), |
154 | @爆破鬼才:吉格斯(Ziggs), |
155 | @时光守护者:基兰(Zilean), |
156 | @荆棘之兴:婕拉(Zyra)].mutableCopy; |
157 | }</span> |
#pragma mark 协议中的方法
1 | <span style= "font-size: medium;" >- ( void )updateSearchResultsForSearchController:(UISearchController *)searchController{ |
2 |
3 | [self.searchResults removeAllObjects]; |
4 | //NSPredicate 谓词 |
5 | NSPredicate *searchPredicate = [NSPredicate predicateWithFormat: @self contains[cd] %@,searchController.searchBar.text]; |
6 | self.searchResults = [[self.items filteredArrayUsingPredicate:searchPredicate]mutableCopy]; |
7 | //刷新表格 |
8 | [self.tableView reloadData]; |
9 |
10 | }</span> |
#pragma mark - Table view data source
1 | <span style= "font-size: medium;" >- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { |
2 |
3 | // Return the number of sections. |
4 | return 1 ; |
5 | } |
6 |
7 | - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { |
8 |
9 | return (!self.searchController.active) ? self.items.count : self.searchResults.count; |
10 | }</span> |
返回cell:
1 | <span style= "font-size: medium;" >- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { |
2 | UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kTableCell forIndexPath:indexPath]; |
3 |
4 | if (cell == nil) { |
5 | cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kTableCell]; |
6 | } |
7 |
8 | if (indexPath.row > 0 ) { |
9 | cell.imageView.frame = CGRectMake( 60 , 60 , 10 , 10 ); |
10 | cell.imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@%ld,indexPath.row + 43 ]]; |
11 | cell.textLabel.font = [UIFont systemFontOfSize: 15 ]; |
12 | } |
13 | cell.textLabel.text = (!self.searchController.active) ? self.items[indexPath.row] : self.searchResults[indexPath.row]; |
14 |
15 | return cell; |
16 | }</span> |
业务代理返回高:
1 | <span style= "font-size: medium;" >- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{ |
2 | if (indexPath.row == 0 ) { |
3 | return 100 ; |
4 | } else { |
5 | return 80 ; |
6 | } |
7 | }</span> |
最终效果:
本文到此结束,需要的朋友可以参考下。