使用UISearchController 实现搜索框
来源:互联网 发布:硝酸甘油软膏 知乎 编辑:程序博客网 时间:2024/05/16 15:57
在iOS8.0之后UISearchDisplayController 终于退出了历史舞台,apple进而推出了新的搜索方法
UISearchController 我们该如何 使用他呢
话不多说,直接看代码
首先 我们创建ViewController类
属性
@property (nonatomic, strong)UISearchController *searchController;
@property (nonatomic, strong)UITableView *titleTableView;
@property (nonatomic, strong)NSMutableArray *filterArray;
@property (nonatomic, strong)NSMutableArray *visableArray;//用来存储页面上面将要显示的数据
@property (nonatomic, strong)NSMutableArray *dataSourceArray;//搜索提醒的时候使用
初始化搜索框
- (void)initWithSearchController
{
self.searchController = [[UISearchController alloc]initWithSearchResultsController:nil];
self.searchController.dimsBackgroundDuringPresentation = NO;
self.searchController.searchBar.placeholder = @"搜索";
[self.searchController.searchBar sizeToFit];
self.searchController.searchResultsUpdater = self;
//将searchBar添加到tableViewHeader上面
self.titleTableView.tableHeaderView = self.searchController.searchBar;
}
//取得数据
- (void)initDataSource
{
self.dataSourceArray = [NSMutableArray array];
self.visableArray = [NSMutableArray array];
for (int i = 0; i < 26; i ++) {
for (int j = 0; j < 4; j ++) {
NSString *str = [NSString stringWithFormat:@"%c%d", 'A' + i, j];
[self.dataSourceArray addObject:str];
}
}
}
#pramark -- mark UISearchControllerDelegate
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//此处的数据也是自己写的 只是为了说明问题
NSString *filter = searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@", filter];
self.visableArray = [NSMutableArray arrayWithArray:[self.dataSourceArray filteredArrayUsingPredicate:predicate]];
[self.titleTableView reloadData];
}
到此为止 一个简单的searchBar就完成了,并且带有提醒输入关键字的功能 ,至于如何将Cancel转化为中文,只需要在plist 文件里面修改就可以了!!! 小弟代码如果有不足之处 请各位不吝赐教,让我们共同进步,thank you!!!
- 使用UISearchController 实现搜索框
- ios搜索框功能的实现/UISearchBar/UISearchController的使用
- IOS-搜索功能实现, UIsearchController的使用
- UISearchController实现搜索
- 不用UIsearchController实现搜索功能
- IOS中UISearchController搜索框筛选功能实现
- UISearchController搜索
- iOS UISearchController TableView 实现简单搜索功能
- 用UISearchController实现微信搜索效果
- UISearchController搜索功能的简单使用
- iOS - UISearchController搜索框与NSPredicate谓词
- iOS开发——搜索框(UISearchController)
- UISearchController搜索框禁止向上移动
- iOS - Swift UISearchController仿微信搜索框
- iOS - Swift UISearchController仿微信搜索框
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)
- UI_搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
- iOS - 搜索框UISearchController的使用(iOS8.0之后替代UISearchBar + UISearchDisplayController的组合)
- Java删除ArrayList中的重复元素的2种方法
- Android Design Support Library常用控件(下)
- C++面试(七)
- linux 配置ip
- 在VS2010调试过程中使用条件断点的一个小技巧
- 使用UISearchController 实现搜索框
- 给 App 提速:Android 性能优化总结
- 【Linux】【驱动】MTD分区表信息解析方法分析
- 使用xFire调用Webservice接口
- Windows 下安装React Native相关问题
- 求特殊自然数
- iOS发布APP到APPstore
- window.onload与$(document).ready()对比
- iOS-AppDelegate默认函数说明