UISearchController(使用、修改cancel文字)
来源:互联网 发布:网络大电影如何赚钱 编辑:程序博客网 时间:2024/06/07 08:36
[_searchController.searchBar setValue:@"取消" forKey:@"_cancelButtonText"];
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它。
NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearchController") __TVOS_PROHIBITED
使用UISearchDisplayController的时候,搜索结果的展示tableView系统已经帮我们封装好,但是使用UISearchController,我们需要提供一个搜索结果的展示TableView.
如何使用UISearchController实现搜索功能呢?
1创建一个搜索结果展示TableViewController
.h文件
#import <UIKit/UIKit.h>@interface SearchResultVC : UITableViewController// 搜索结果数据@property (nonatomic, strong) NSMutableArray *resultsArray;@end
.m
#import "SearchResultVC.h"@interface SearchResultVC ()@end@implementation SearchResultVC- (void)viewDidLoad { [super viewDidLoad];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}#pragma mark - Table view data source- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.resultsArray.count;}- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RESULT_CELL"]; if (cell == nil) { cell = [tableView dequeueReusableCellWithIdentifier:@"RESULT_CELL"]; } cell.textLabel.text = self.resultsArray[indexPath.row]; cell.accessoryType=UITableViewCellAccessoryDisclosureIndicator; return cell;}
2 创建一个UISearchController
- (void)initSearchController{ SearchResultVC *resultTVC = [[SearchResultVC alloc] initWithStyle:UITableViewStylePlain]; UINavigationController *resultVC = [[UINavigationController alloc] initWithRootViewController:resultTVC]; self.searchController = [[UISearchController alloc]initWithSearchResultsController:resultVC]; self.searchController.searchResultsUpdater = self; //self.searchController.dimsBackgroundDuringPresentation = NO; //self.searchController.hidesNavigationBarDuringPresentation = NO; self.searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x,self.searchController.searchBar.frame.origin.y,self.searchController.searchBar.frame.size.width,44); self.tableView.tableHeaderView = self.searchController.searchBar; self.searchController.searchBar.delegate = self;}
3 实现UISearchController的UISearchResultsUpdating方法,当开始搜索的时候响应。并且实现筛选的逻辑
#pragma mark - UISearchResultsUpdating- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{ UINavigationController *navController = (UINavigationController *)self.searchController.searchResultsController; SearchResultVC *resultVC = (SearchResultVC *)navController.topViewController; [self filterContentForSearchText:self.searchController.searchBar.text]; resultVC.resultsArray = self.tempsArray; [resultVC.tableView reloadData];}#pragma mark - Private Method- (void)filterContentForSearchText:(NSString *)searchText{ NSLog(@"%@",searchText); NSUInteger searchOptions = NSCaseInsensitiveSearch | NSDiacriticInsensitiveSearch; [self.tempsArray removeAllObjects]; for (int i = 0; i < self.resultArray.count; i++) { NSString *title = self.resultArray[i]; NSRange storeRange = NSMakeRange(0, title.length); NSRange foundRange = [title rangeOfString:searchText options:searchOptions range:storeRange]; if (foundRange.length) { [self.tempsArray addObject:self.resultArray[i]]; } }}
完成了,就是这么简单,UI什么的可以自定义。简单demo可以去我github上下载: https://github.com/wangdachui/WTUISearchController
0 0
- UISearchController(使用、修改cancel文字)
- 关于iOS修改SearchBar上Cancel文字颜色
- 把UISearchController的cancel按钮自定义中文取消
- iOS UISearchController的使用
- iOS UISearchController的使用
- 8.0系统 UISearchController 使用
- UISearchController的基本使用
- iOS UISearchController使用
- UISearchDisplayController “No Results“ cancel修改
- iOS 修改UISearchBar cancel 按钮
- UISearchController
- UISearchController
- UISearchController
- UISearchController
- UISearchController
- UISearchController
- UIsearchController
- UIsearchController
- NDK学习笔记1
- 我的个人博客开通啦,欢迎来吐槽,有何问题可发邮件到1207012718@qq.com反馈,谢谢~
- Jackson实现Object对象与Json字符串的互转
- 使用pod install发生的错误:[!] The 'XXX' target has frameworks with conflicting names: ummo
- Factorialize a Number
- UISearchController(使用、修改cancel文字)
- RabbitMQ~广播消息
- cookie和session的区别
- python基础学习-输入输出
- 从2段已序的数组中查找某一个数的算法
- 两个div右侧固定,左侧自适应屏幕
- I2C总线协议学习笔记
- 先存储逆序后的整数再输出
- 【RxJava】种下一棵树RxJava