UISearchController(使用)
效果图1
效果图2
其实一般是在第一种情况使用的UISearchController,但是第二种情况这种又懒得去用uisearchbar+uitableview等等去处理,
其实主要是对数据源的合理使用即可
UISearchController通常是用来排查数据的,比如当前的uitableviewcontroller有许多数据,排查之后需要更新显示数据。
首先UISearchController是联通主cntroller与结果controller桥梁
在主controller建立UISearchController对象
@interface UIViewControllerLocation ()
{
UISearchController* searchController0;
}
那么如何联通主controller
CGRect rectBar=CGRectMake(0, 0, self.view.frame.size.width, 44);
[searchController0.searchBar setFrame:rectBar];
[self.searchBar addSubview:searchController0.searchBar]; 以上代码需要理解的是searchController0.searchBar是一个只读属性,所以不能被assign,
self.searchBar是一个view
/**
* 搜索框
*/
@property (weak, nonatomic) IBOutlet UIView *searchBar;
不能够直接使用(UISearchBar)self.searchBar=searchController0.searchBar;只有tableview.tableHeaderView=searchController0.searchBar才有效果,同样是view不知道为什么
还有比较重要的一点就是,即使模拟器可以正常的显示,但是真机却无法显示searchBar(xcode7+iphone5s+ios8.3)
最终调试得出真机运行时searchController0.searchBar的frame是(0 0,0 0)需要重置frame才可以正常显示
那么如何联通结果controller
@interface UIViewControllerLocation ()
{
SearchResult* searchResult;
}
searchResult=[SearchResult new];
searchController0=[[UISearchController alloc] initWithSearchResultsController:searchResult];
SearchResult是一个uitableviewcontroller
然后是主controller作为了结果controller的数据更新者
searchController0.searchResultsUpdater=self;
需要遵循一个代理
@interface UIViewControllerLocation : UIViewController<UISearchResultsUpdating>
然后
//设置活跃性为YES否则需要自行推送结果controller
searchController0.active=YES;
那么主controller如何推送数据到 结果 controller
代理
/**
* 当搜索框开始改变时触发
*
* @param searchController
*/
-(void)updateSearchResultsForSearchController:(nonnull UISearchController *)searchController
{
searchResult.getResult(@[@,@,@,@,@,@]);
}
结果controller需要处理接受到的数据(使用的block)
#import <UIKit/UIKit.h>
#import "NSObjectDataSource.h"
/**
* 获取数据
*
* @param idData
*/
typedef void (^GetResult)(id idData);
@interface SearchResult : UIViewController
/**
* 获取数据的block
*/
@property(nonatomic,copy)GetResult getResult;
/**
* 查询数据列表
*/
@property (weak, nonatomic) IBOutlet UITableView *tabelview;
/**
* tableview的datasource代理
*/
@property(nonatomic,retain)NSObjectDataSource* dataSource;
@end
主controller里初始化结果controller的获取数据block
SearchResult*__weak weakSearchResult=searchResult;
searchResult.getResult=^(NSArray* array)
{
weakSearchResult.dataSource.nsArrayModels=array;
[weakSearchResult.tabelview reloadData];
};
在主controller里搜索更新时执行就可以了(只传递最终数据,数据的筛选需要自行处理)
UISearchController(使用)的更多相关文章
- UISearchController 的用法[点击搜索框,自动到顶部]
//在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...
- iOS UISearchController的使用
在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- UISearchController使用
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索 iOS8提供实现搜索功能的SDK:UISearchController(iOS8.0之后).UIS ...
- UItableview 添加 uisearchController
@property (nonatomic, strong) UISearchController* searchController; self.searchController = [[UISear ...
- UISearchController
搜索框UISearchController的使用(iOS8.0以后替代UISearchBar + UIS) 1.在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便 ...
- UITableView与UISearchController搜索及上拉加载,下拉刷新
#import "ViewController.h" #import "TuanGouModel.h" #import "TuanGouTableVi ...
- iOS原生的搜索:UISearchController
iOS8之前我们使用UISearchDisplayController做TableView的本地搜索,查看UIKit库,苹果已经使用新控件取代它. NS_CLASS_DEPRECATED_IOS(3_ ...
- UISearchController Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
Attempting to load the view of a view controller while it is deallocating is not allowed and may res ...
- iOS8以后 UISearchController的用法
查了不少资料,都不太全,自己查看了apple文档,写了一份代码: 如下(只是界面): 1. 声明属性 @property (nonatomic, strong) UISearchController ...
随机推荐
- Java基础10-循日历制作
编写过程:先指定固定的年份来输出指定月份的日历 /*计算1900年1月1日到2018年11月1日一共有多少天 计算月份 对7进行取模,结果就为星期几 */ import java.util.Scann ...
- hduoj 2955Robberies
Robberies Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total ...
- 【AngularJs】---angular-chart.js 图表报表
移步至我在百度经验的这篇文章: http://jingyan.baidu.com/article/a378c960b26636b3282830ad.html
- js对象动态赋值
<view class="movies-template"> <template is="movieListTemplate" data=&q ...
- MemoryCache缓存 ---缓存时效
MemoryCache缓存 ---缓存时效测试 var cachePool = new MyCachePool(); //Thread.Sleep(1000); var value = cachePo ...
- asp.net 单元测试(转)
最早接触单元测试是看了极限编程相关资料里边讲的测试驱动开发,然后下载了Nunit研究了一下,但并没产生多大的触动,因为那个时候做的都是些时间紧任务重的事情,对于单元测试的直接感觉就是有可能比较费时间. ...
- 翻String.Format源码发现的新东西:StringBuilderCache
起因: 记不清楚今天是为毛点想F12看String.Format的实现源码了,反正就看到了下图的鸟东西: 瞬间石化有没有,StringBuilder还能这么获取? 研究StringBuilderCac ...
- Spring-cloud学习线路
学习本学习路线学习完,大家将会对微服务.Spring Cloud.Docker.Kubernetes有一个系统.全面的认识.通过学习,将能掌握相关的知识体系,并能够投入到项目实战中去. 本学习路线采用 ...
- oracle基本查询练习
select * from regions; select * from countries; select * from locations; select * from departments; ...
- [C#]为什么Interface里的成员不能使用static修饰?
首先引用MSDN里的原文 Interface members are automatically public, and they can't include any access modifiers ...