ios8以后的搜索栏
来源:互联网 发布:画画软件手机版 编辑:程序博客网 时间:2024/06/04 21:43
在重新看以前的入门书时,发现很多API在ios8以后都弃用了。今天是涉及到搜索栏。
在官方文档中可以看到,UISearchDisplayController连同UISearchDisplayDelegate在ios8以后被弃用了,以后用UISearchController替代。
@interface ViewController : UIViewController < UITableViewDelegate , UITableViewDataSource ,UISearchBarDelegate , UISearchResultsUpdating >
@property(nonatomic , retain ) NSArray *recipes;// 源数据
@property(nonatomic , retain ) NSMutableArray *searchList;// 用于保存搜索结果的数组
@property(nonatomic , retain ) IBOutlet UITableView *tableview;
@property(nonatomic , retain ) UISearchController *sC; // 本来叫searchController的,但与后面有个方法里的参数重名了 ,遂改成这个2B名字
@end
-(void) viewDidLoad{
……
sC = [ [ UISearchController alloc ] initWithSearchResultsController : nil ];
sC.searchResultsUpdater = self;
sC.dimsBackgroundDuringPresentation = NO;// 这两个属性也可改为YES
sC.hidesNavigationBarDuringPresentation = NO;
sC.searchBar.frame = CGRectMake ( sC.searchBar.frame.origin.x , sC.searchBar.frame.origin.y , sC.searchBar.frame.size.width , 44.0) ;
tableview.tableHeaderView = sC.searchBar;// 是用代码添加的搜索栏,在故事板中拖控件拉线拉不上……
}
-numberOfRowsInsection{
if ( sC.active ){// 新版的不再是对tableview做判断,直接用active属性
return [ searchList count ] ;
}else {
return [ recipes count ] ;
}
}
-cellForRowAtIndexPath{
……
if ( sC.active ){
cell.textLabel.text = [ searchList objectAtIndex : indexPath.row ] ;
}else{
cell.textLabel.text = [ recipes objectAtIndex : indexPath.row ];
}
return cell;
}
-prepareForSegue{
if ( [ segue.identifier isEqualToString:@"showRecipeDetail" ] ){
RecipeViewController *destViewController = segue.destinationViewControlelr;
NSIndexPath *indexPath = [ tableview indexPathForSelectedRow ] ;
if( sC.active ){
destViewController.recipeName = [ searchList objectAtIndex:indexPath.row ] ; // 不然选择的结果会出错
}else{
destViewController.recipeName = [ recipes objectAtIndex:indexPath.row ] ;
}
]
}
-updateSearchResultsForSearchController{
NSString *searchString = [ searchController.searchBar text ] ;
NSPredicate *predicate = [ NSPredicate predicateWithFormat:@"SELF CONTAINS [cd] %@",searchString ] ;
if( searchList != nil ){
[ searchList removeAllObjects ] ;
}
searchList = [ NSMutableArray arrayWithArray: [ recipes filteredArrayUsingPredicate:predicate ] ] ;
[ tableview reloadData ] ;
}
拖控件的暂时没搞清楚怎么写
- ios8以后的搜索栏
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController的组合)
- UI_搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UISearchDisplayController)
- ios8以后的一种即时渲染
- iOS8以后的屏幕宽高变化
- iOS8以后的屏幕宽高变化
- IOS8下的数据搜索
- 修改iOS8以后tableView的线右移的问题
- UIAlertView和UIActionSheet类在IOS8以后的改动
- 关于iOS8之前和以后的相册图片获取实现
- ios8以后图标尺寸和iOS8之后的注意事项(持续更新...)
- ios8以后Label自适应大小
- UISearchController IOS8之后推荐使用的搜索控件
- iOS8以后第三方键盘,获取高度为0的问题
- 关于ios8以后BitCode一词,关于在xcode7(bate3)中报错的解决以及普及
- iOS8上面正常的,到了iOS9以后,应用连接不上服务器,这是为什么?
- iOS8以后推送通知需要访问权限
- ios8以后要自己开启定位权限
- mac常用命令
- Android Studio插件整理
- Mongodb常用语句
- mac快捷键
- 面试题28—相关题目(字符所有的组合)
- ios8以后的搜索栏
- chrome console调试(寻找指定src的img)
- 程序判断Windows用户帐户密码为空的方法
- 35 个 Java 代码性能优化总结
- 关于ListView的BaseAdapter中getView方法问题
- 理解@ComponentScan注解
- 深入分析java线程池的实现原理
- 回归的线性模型
- 棋盘覆盖