您的位置:首页 > 产品设计 > UI/UE

IOS UI学习 UISearchController

2015-09-17 20:19 405 查看
使用UISearchController 配合UITableView实现搜索功能

#import "ViewController12.h"

@interface ViewController12 () <UITableViewDataSource , UITableViewDelegate , UISearchResultsUpdating>

@end

@implementation ViewController12
{
UISearchController * _searchC;//SearchController
UITableView *_tableV; //tableView
NSMutableArray * _selectArr;//存放搜索结果数组
NSMutableArray *_dataArr;//存放所有数据的数组
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//是否根据按所在界面的navigationbar与tabbar的高度,自动调整scrollview的 inset,设置为no,让它不要自动调整就
self.automaticallyAdjustsScrollViewInsets = NO;
[self createData];
[self createTableView];
_selectArr = [[NSMutableArray alloc] init];
}

#pragma mark 创建数据
-(void)createData
{
if (!_dataArr)
{
_dataArr = [[NSMutableArray alloc] init];
}

for (NSInteger i = 0; i<100; i++)
{
[_dataArr addObject:[NSString stringWithFormat:@"%ld",i]];
}
NSLog(@"%@",_dataArr);
}

#pragma mark 创建 TableView  UISearchController
-(void)createTableView
{
_tableV = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height-64-54)];

_tableV.delegate = self;
_tableV.dataSource = self;
_searchC = [[UISearchController alloc] initWithSearchResultsController:nil];

_searchC.hidesNavigationBarDuringPresentation = NO;
_searchC.dimsBackgroundDuringPresentation = YES;
//设置代理
_searchC.searchResultsUpdater = self;
//调整SearchBar尺寸为自适应
[_searchC.searchBar sizeToFit];
//把SearchBar 给 TableView的标头
_tableV.tableHeaderView = _searchC.searchBar;
[self.view addSubview:_tableV];

}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//通过active属性判断是否搜索
if (_searchC.active)
{
return _selectArr.count;
}
else
return _dataArr.count;
}

//设置单元格 cell
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString * str = @"cell";
UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:str];
if (!cell)
{
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
}

if (_searchC.active)
{
cell.textLabel.text = _selectArr[indexPath.row];
}
else
{
cell.textLabel.text = _dataArr[indexPath.row];
}
return cell;
}

//执行搜索
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString = [_searchC.searchBar text];
NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString];
if (_selectArr!= nil) {
[_selectArr removeAllObjects];
}
//过滤数据
_selectArr= [NSMutableArray arrayWithArray:[_dataArr filteredArrayUsingPredicate:preicate]];
//刷新表格
[_tableV reloadData];
}

- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: