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
相关文章推荐
- [翻译] LiquidFloatingActionButton
- PTA 02-线性结构3 Pop Sequence (使用STL)
- ipad开发中UIPopoverController中显示自定义view尺寸显示问题
- 关于request.getParameter()与request.setAttribute()的区别
- 2299 Ultra-QuickSort(归并)
- Request的getParameter和getAttribute方法的区别
- iOS 开发学习 导航控制器(UINavigationController)
- CodeForces 12C - Fruits
- IOS UI学习 UI 十个小控件 初度学习
- poj 2749 Building roads(2-sat)
- hdu 1242 Rescue 搜索 bfs 优先队列
- ipad开发中UIPopoverController中自定义view在Xcode6中尺寸匹配问题
- UVa 1626:Brackets Sequence(DP)
- UIday1801:沙盒三个文件的路径的获取、简单与复杂对象的读写、NSUserDefaults、NSFileManager
- 拖拽UITextView退出键盘
- APK安装出错:INSTALL_FAILED_UID_CHANGED
- android ui分析 - 今日头条UI整体结构
- IOS每天15个注意点系列之UI-应用管理
- wpf探秘之B哥Dispatcher私房菜(2)最终回
- UICollectionView