iOS8 SearchBar相关
来源:互联网 发布:手机最好个人房源软件 编辑:程序博客网 时间:2024/04/28 04:30
UISearchDisplayController被iOS8废止,取而代之的是 UISearchController
本文转载自: http://my.oschina.net/u/1999967/blog/309511
苹果官方:UISearchController
0.initWithSearchResultsController:
可以为nil,表示使用当前的 viewController。也可以使用自定义的其他viewcontroller。
self.mySearchController = [[UISearchController alloc] initWithSearchResultsController:nil];
1.searchResultsUpdater:设置显示搜索结果的控制器。
_mySearchController.searchResultsUpdater = self;
2.dimsBackgroundDuringPresentation:设置开始搜索时背景显示与否
_mySearchController.dimsBackgroundDuringPresentation = NO;
3.[searchBar sizeToFit]:设置searchBar位置自适应
[_mySearchController.searchBar sizeToFit];
4.设置searchBar为UITableView的头部视图
self.myTableView.tableHeaderView = self.mySearchController.searchBar;
5.UISearchResultsUpdating:代理方法 -(
void
)updateSearchResultsForSearchController:(UISearchController *)searchController
话不多说,直接上代码
@interface ViewController () @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 ViewController - (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];}
当需要使用Push,现实详细信息等情况的时候,需要将searchBar隐藏。可以将active属性设置为NO。
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
if ([segue.identifier isEqualToString:@"tablePush"])
{
if ([segue.destinationViewController isKindOfClass:[CodePushViewController class]])
{
CodePushViewController *destVC = (CodePushViewController *)segue.destinationViewController;
destVC.labelName = [showList objectAtIndex:[[tablesView indexPathForSelectedRow] row]];
searchC.active = NO ;
}
}
}
- iOS8 SearchBar相关
- SearchBar
- iOS8 TabBar图片相关问题
- iOS8新特性(iTunes相关)
- 自定义searchBar
- 关于searchBar
- ios8
- iOS8
- iOS8
- iOS8
- iOS8
- iOS8
- searchbar的内容
- 给TableView增加SearchBar
- 去掉searchBar边框
- SearchBar 的触发事件
- 导航中设置searchBar
- 关于searchBar的问题
- 清除svn用户名密码
- java多线程总结
- [BZOJ3123][Sdoi2013]森林:可持久化线段树+启发式合并
- ArcGIS教程:直方图
- 常用设计模式之工厂方法模式、抽象工厂模式
- iOS8 SearchBar相关
- xib初识
- [Spring笔记]Spring 4.1.2 Jar文件描述
- Ubuntu 14.04+Apache2.4 + MySQL环境搭建
- 下拉刷新新样式
- HDU 4455 Substrings
- 浅析Lua中table的遍历(转)
- hibernate sqlQuery mysql别名问题
- 利用css的clear属性实现文字环绕图片效果