網站首頁 編程語言 正文
最近自己在寫一個APP,其中需要實現搜索框搜索功能,于是乎就想寫篇博客介紹下UISearchController和搜索框的實現。
我寫的是一個天氣預報APP,直接以我APP中的源代碼來詳細介紹下搜索框的實現。
注:在iOS 8.0以上版本中, 我們可以使用UISearchController來非常方便地在UITableView中添加搜索框. 而在之前版本中, 我們還是必須使用UISearchBar + UISearchDisplayController的組合方式。
初始化UISearchController
- (void)viewDidLoad {
? ? [super viewDidLoad];
? ? self.searchController = [[UISearchController alloc] initWithSearchResultsController:nil];
? ? self.searchController.searchResultsUpdater = self;
? ? self.searchController.dimsBackgroundDuringPresentation = false;
? ? [self.searchController.searchBar sizeToFit];
? ? self.tableView.tableHeaderView = self.searchController.searchBar;
}
使用UISearchController要繼承UISearchResultsUpdating協議, 搜索必須實現UISearchResultsUpdating方法.
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController {
? ? [self.searchList removeAllObjects];
? ? //在iOS開發(fā)中,系統(tǒng)提供了NSPredicate這個類給我們進行一些匹配、篩選操作
? ? NSPredicate *searchPredicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", self.searchController.searchBar.text];
? ? self.searchList = [[self.dataList filteredArrayUsingPredicate:searchPredicate] mutableCopy];
? ? dispatch_async(dispatch_get_main_queue(), ^{
? ? ? ? [self.tableView reloadData];
? ? });
}
通過UISearchController的active屬性來判斷輸入框是否處于active狀態(tài),然后更新UITableview
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
? ? return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
? ? if (!self.searchController.active) {
? ? ? ? return self.dataList.count;
? ? }
? ? else{
? ? ? ? return self.searchList.count;
? ? }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
? ? static NSString *ID = @"cell";
? ? UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
? ? if (!cell) {
? ? ? ? cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
? ? }
? ? if (!self.searchController.active) {
? ? ? ? cell.textLabel.text = self.dataList[indexPath.row];
? ? }
? ? else{
? ? ? ? cell.textLabel.text = self.searchList[indexPath.row];
? ? }
? ? return cell;
}
搜索完之后,將搜索框移除
- (void)viewWillDisappear:(BOOL)animated {
? ? [super viewWillDisappear:animated];
? ? if (self.searchController.active) {
? ? ? ? self.searchController.active = NO;
? ? ? ? [self.searchController.searchBar removeFromSuperview];
? ? }
}
效果圖如下:
原文鏈接:https://ahtchxw.blog.csdn.net/article/details/51581697
相關推薦
- 2023-01-14 python與matlab一些常用函數互轉問題_python
- 2022-11-14 C語言?ffmpeg與sdl實現播放視頻同時同步時鐘詳解_C 語言
- 2023-11-26 數據結構:數組—特殊矩陣的壓縮存儲
- 2022-04-14 Python中創(chuàng)建表格詳細過程_python
- 2023-10-10 Promise同時獲取n個接口數據的幾種方式
- 2022-04-12 Redis?Server啟動過程的詳細步驟_Redis
- 2023-01-05 Kotlin注解與反射的定義及創(chuàng)建使用詳解_Android
- 2022-10-13 解析django的csrf跨站請求偽造_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發(fā)現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支