搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
2016-02-02 15:23
579 查看
1.searchResultsUpdater:设置显示搜索结果的控制器
?
2.dimsBackgroundDuringPresentation:设置开始搜索时背景显示与否
?
3.[searchBar sizeToFit]:设置searchBar位置自适应
?
4.设置searchBar为UITableView的头部视图
?
5.UISearchResultsUpdating:代理方法
#import "SearchViewController.h"
@interface ShareViewController ()<UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *visableArray;//可见的
@property (nonatomic, strong) NSMutableArray *filterArray;//滤波器
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) UISearchController *mySearchController;
@end
@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initial];
}
- (void)initial{
self.dataSourceArray = [NSMutableArray array];
self.filterArray = [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];
}
}
self.visableArray = self.dataSourceArray;
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_mySearchController.searchResultsUpdater = self;
_mySearchController.dimsBackgroundDuringPresentation = NO;
[_mySearchController.searchBar sizeToFit];
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (!_visableArray || _visableArray.count == 0) {
_visableArray = _dataSourceArray;
}
return _visableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
}
cell.textLabel.text = [_visableArray objectAtIndex:indexPath.row];
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *filterString = searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@", filterString];
self.visableArray = [NSMutableArray arrayWithArray:[self.dataSourceArray filteredArrayUsingPredicate:predicate]];
[self.myTableView reloadData];
}
?
?
?
?
#import "SearchViewController.h"
@interface ShareViewController ()<UISearchResultsUpdating,UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *myTableView;
@property (nonatomic, strong) NSMutableArray *visableArray;//可见的
@property (nonatomic, strong) NSMutableArray *filterArray;//滤波器
@property (nonatomic, strong) NSMutableArray *dataSourceArray;
@property (nonatomic, strong) UISearchController *mySearchController;
@end
@implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self initial];
}
- (void)initial{
self.dataSourceArray = [NSMutableArray array];
self.filterArray = [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];
}
}
self.visableArray = self.dataSourceArray;
self.myTableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
_myTableView.delegate = self;
_myTableView.dataSource = self;
[self.view addSubview:_myTableView];
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
_mySearchController.searchResultsUpdater = self;
_mySearchController.dimsBackgroundDuringPresentation = NO;
[_mySearchController.searchBar sizeToFit];
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
if (!_visableArray || _visableArray.count == 0) {
_visableArray = _dataSourceArray;
}
return _visableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"identifier"];
if (!cell) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"identifier"];
}
cell.textLabel.text = [_visableArray objectAtIndex:indexPath.row];
return cell;
}
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController{
NSString *filterString = searchController.searchBar.text;
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF contains [c] %@", filterString];
self.visableArray = [NSMutableArray arrayWithArray:[self.dataSourceArray filteredArrayUsingPredicate:predicate]];
[self.myTableView reloadData];
}
相关文章推荐
- Discuz--QQ互联登陆出现(1054) Unknown column “conuintoken” in “field list”
- UILabel 设置字体间的距离 和 行与行间的距离
- Cts框架解析(8)-IBuildProvider
- 2016你一定要试试这8款原型设计工具
- Buildings
- iOS8中提示框的使用UIAlertController(UIAlertView和UIActionSheet二合一)
- 模糊搜索UISearchBar
- 基于对话框的UPDATE_COMMAND_UI的响应问题
- 一張圖勝千言萬語,13 張圖徹底解釋 UI & UX 差別是什麼
- 瀑布流(UIScrollView实现)
- RMQ (Range Minimum/Maximum Query)算法
- Build模式——自由扩展
- Builder模式
- setCellValue 后面的数据被前面的数据覆盖
- UIViewController+UITableView 和 UITableViewController
- Interface Builder一些使用技巧
- iOS UITabBarController
- 生成uuid
- Cannot create TypedQuery for query with more than one return using requested result type
- Wunder Fund Round 2016 (Div. 1 + Div. 2 combined)-B. Guess the Permutation(模拟)