網站首頁 編程語言 正文
最近自己在寫一個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開發中,系統提供了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狀態,然后更新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
相關推薦
- 2022-10-06 Android開發Jetpack組件LiveData使用講解_Android
- 2022-07-28 Go語言反射reflect.Value實現方法的調用_Golang
- 2022-06-30 python神經網絡MobileNet模型的復現詳解_python
- 2022-08-02 C#中的延時函數sleep_C#教程
- 2022-05-12 Kotlin 代數/枚舉/密封類
- 2022-06-29 docker容器狀態轉換管理命令實例詳解_docker
- 2022-05-13 Scrapy-middlewares對象
- 2024-03-08 Linux虛擬機輸入ifconfig不顯示IP地址解決方法
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支