iOS搜索框
//用于存储搜索结果的数组
@property(nonatomic,strong)NSMutableArray *resultProducts;
- (NSArray *)allProducts{
if(!_allProducts){
_allProducts = [Product demoData];
}
return _allProducts;
{
[super viewDidLoad];
self.resultProducts = [NSMutableArray array];
// 为用于展示搜索结果的表格注册单元格
[self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
}
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==self.searchDisplayController.searchResultsTableView){
return self.resultProducts.count;
}else{
return self.allProducts.count;
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
Product *p = nil;
if(tableView==self.tableView){
p = self.allProducts[indexPath.row];
}else{
p = self.resultProducts[indexPath.row];
}
cell.textLabel.text = p.name;
return cell;
- (BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchString:(NSString *)searchString{
NSMutableArray *tempArray = [NSMutableArray array];
//获取此时在searchBar的scope中哪个按钮被选中
NSInteger selectedScope = self.searchDisplayController.searchBar.selectedScopeButtonIndex;
for (Product *p in self.allProducts) {
//判断输入的searchString是否在p的name中出现了,NSRange包含两部分,一个是location,一个是length,location记录的时在name中包含searchString的位置,length记录占用的长度;通过判断range中得length,如果长度为0 则代表name不包含search。
NSRange range = [p.name rangeOfString:searchString];
if(range.length>0 && p.type==selectedScope){
[tempArray addObject:p];
}
}
self.resultProducts = tempArray;
return YES;
}
//选定的scope中的按钮发生改变时,执行该方法,参数searchOption代表的就是当前被选中的scope
//按钮的索引
-(BOOL)searchDisplayController:(UISearchDisplayController *)controller shouldReloadTableForSearchScope:(NSInteger)searchOption{
NSMutableArray *tempArray = [NSMutableArray array];
//获取此时在searchBar中输入的文本
NSString *text = self.searchDisplayController.searchBar.text;
for (Product *p in self.allProducts) {
//判断输入的searchString是否在p的name中出现了,NSRange包含两部分,一个是location,一个是length,location记录的时在name中包含searchString的位置,length记录占用的长度,通过判断range中得length,如果长度为0 则代表name不包含search
NSRange range = [p.name rangeOfString:text];
if(range.length>0 && p.type==searchOption){
[tempArray addObject:p];
}
}
self.resultProducts = tempArray;
return YES;



#import "Product.h"
#import "SearchResultTableViewController.h"
//用于显示没有搜索动作以前的全部数据的表视图控制器
@interface MainTableViewController ()<UISearchResultsUpdating,UISearchBarDelegate>
@property(nonatomic,strong)NSArray *allProducts;
//用于控制所有结果显示的控制器
@property(nonatomic,strong)UISearchController *searchController;
if(!_allProducts){
_allProducts = [Product demoData];
}
return _allProducts;
self.searchController = [[UISearchController alloc]initWithSearchResultsController:self.showResultViewController];
//设置搜索控制器的结果更新时由谁代理
self.searchController.searchResultsUpdater = self;
//设置显示的搜索bar的大小和样式
[self.searchController.searchBar sizeToFit];
self.searchController.searchBar.scopeButtonTitles=@[@"设备",@"软件",@"其他"];
//将搜索bar添加到表头视图中
self.tableView.tableHeaderView = self.searchController.searchBar;
//设置是否在数据发现变更时,允许切换控制器
self.definesPresentationContext = YES;
//为了获取scope按钮被改变这个时机,需要设置搜索框的代理
//searchBar中包含一个文本框和一个分段控件
self.searchController.searchBar.delegate = self;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.allProducts.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
Product *p = self.allProducts[indexPath.row];
cell.textLabel.text = p.name;
return cell;
//用户输入的搜索文本
NSString *text = searchController.searchBar.text;
//用户选择的scope按钮
NSInteger selectedIndex = searchController.searchBar.selectedScopeButtonIndex;
NSMutableArray *temp = [NSMutableArray array];
for (Product *p in self.allProducts) {
NSRange range = [p.name rangeOfString:text];
if(range.length>0 && p.type==selectedIndex){
[temp addObject:p];
}
}
//将搜索结果传给用于展示结果的那个控制器
self.showResultViewController.resultArray = [temp copy];
#pragma mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
[self updateSearchResultsForSearchController:self.searchController];
[super viewDidLoad];
[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell2"];
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.resultArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2" forIndexPath:indexPath];
Product *p = self.resultArray[indexPath.row];
cell.textLabel.text = p.name;
return cell;
iOS搜索框的更多相关文章
- iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS 搜索框控件 最简单的dome
刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...
- iOS搜索框UISearchBar
当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...
- iOS搜索框UISearchBar 分类: ios技术 2015-04-03 08:55 82人阅读 评论(0) 收藏
当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...
- IOS搜索框输入中文解决方案(防抖)
class Header extends React.Component { constructor(props) { super(props); this.time = 0; // 重点在于这个th ...
- iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)
在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...
- iOS开发——UI篇OC篇&TextField作为搜索框的使用
TextField作为搜索框的使用 在iOS开发中我们经常会使用到搜索框,但是有的时候系统自带的搜索框不足以满足我吗想要的功能,这个时候我们就可以使用自定义的搜索框实现想要的功能. 今天就简单的介绍一 ...
- 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框
-iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之使用TextField作为搜索框 今天给大家带来一个新的技巧,比如平时我们要使用代码创建一 ...
- ios UISearchBar搜索框的基本使用
摘要: 小巧简洁的原生搜索框,漂亮而易用,如果我们的应用没有特殊需求,都可以使用它. iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建 ...
随机推荐
- 【Hadoop代码笔记】Hadoop作业提交之Child启动reduce任务
一.概要描述 在上篇博文描述了TaskTracker启动一个独立的java进程来执行Map任务.接上上篇文章,TaskRunner线程执行中,会构造一个java –D** Child address ...
- delphi 单例模式实现
unit Unit2; interface uses System.SysUtils; type { TSingle } TSingle = class(TObject) private FStr: ...
- Unity中的单实例
static GUIManager myInstance; public static GUIManager Instance { get { if (myInstance == null) myIn ...
- jemalloc优化MySQL、Nginx内存管理
上一篇文章<TCMalloc优化MySQL.Nginx.Redis内存管理>,下面来看下jemalloc jemalloc源于Jason Evans 2006年在BSDcan confer ...
- Hibernate之HQL查询
一.Hibernate 提供了以下几种检索对象的方式: 导航对象图检索方式: 根据已经加载的对象导航到其他对象 OID 检索方式: 按照对象的 OID 来检索对象 HQL 检索方式:使用面向对象的 H ...
- 提高iOS开发效率的方法和工具
http://www.cocoachina.com/ios/20150717/12626.html 介绍 这篇文章主要是介绍一下我在iOS开发中使用到的一些可以提升开发效率的方法和工具. IDE 首先 ...
- Android问题-新电脑新系统WIN764位上安装简版本的XE8提示“Unit not found: 'System'”
问题现象:电脑太慢,重安新系统,所以要安DELPHIXE8,但安装过程中出现二次杀软件提示,我都选的是通过.但是XE8过程到最后的"Create AVD"时出现一个错误(具体是什么 ...
- Spring Autowiring by Name
In Spring, "Autowiring by Name" means, if the name of a bean is same as the name of other ...
- 用ALAssetsLibrary将过滤后图片写入照片库
转载自:http://blog.sina.com.cn/s/blog_61235faa0100z3dp.html CIImage *saveToSave = [filter outputImage]; ...
- BaiduMap开发,获取公交站点信息。
可能有些人会出现无法导入overlayutil的错误,这是因为BaiduMap里面的包把这部分删除掉了,并且官方没有给出说明,这个地方以前也是让我折腾了很久. 不知道现在有没有说明这个问题,如果需要这 ...