在iOS8以前搜索框是作为一个控件添加到TableViewController中,
有系统自带的搜索变量self.searchDisplayController
遵守一个搜索显示的协议<UISearchDisplayDelegate>,实现方法就可以了。
在ProductTableViewController.m文件中
#import "ProductTableViewController.h"
#import "Product.h"
@interface ProductTableViewController ()<UISearchDisplayDelegate>
@property(nonatomic,strong)NSArray *allProducts;
//用于存储搜索结果的数组
@property(nonatomic,strong)NSMutableArray *resultProducts;
@end
@implementation ProductTableViewController
- (NSArray *)allProducts{
    if(!_allProducts){
        _allProducts = [Product demoData];
    }
    return _allProducts;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.resultProducts = [NSMutableArray array];
    // 为用于展示搜索结果的表格注册单元格
    [self.searchDisplayController.searchResultsTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
   
}
#pragma mark - Table view data source
//实现tableView的三问
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    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;
}
实现协议:
//只要在搜索框中修改搜索的内容,立即调用此方法,参数searchString就是用户此时在搜索框中输入的文本
- (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;
}
 
在iOS8中,搜索框,是作为一个组成部分,聚合方式
搜索框结构图:
 
在MainTableViewController.m文件中
#import "MainTableViewController.h"
#import "Product.h"
#import "SearchResultTableViewController.h"
//用于显示没有搜索动作以前的全部数据的表视图控制器
@interface MainTableViewController ()<UISearchResultsUpdating,UISearchBarDelegate>
@property(nonatomic,strong)NSArray *allProducts;
//用于控制所有结果显示的控制器
@property(nonatomic,strong)UISearchController *searchController;
//用于展示搜索结果的控制器,是自定义的用于展示结果的那个控制器
@property(nonatomic,strong)SearchResultTableViewController *showResultViewController;
@end
@implementation MainTableViewController
- (NSArray *)allProducts{
    if(!_allProducts){
        _allProducts = [Product demoData];
    }
    return _allProducts;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.showResultViewController = [[SearchResultTableViewController alloc]init];
    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;
}
#pragma mark - 主TableViewController的tableView内容设置,回答三问
- (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;
}
#pragma mark - UISearchResultsUpdating
//实现协议中的方法:更新搜索内容
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController{
    //用户输入的搜索文本
    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];
    [self.showResultViewController.tableView reloadData];
}
#pragma  mark - UISearchBarDelegate
- (void)searchBar:(UISearchBar *)searchBar selectedScopeButtonIndexDidChange:(NSInteger)selectedScope{
    [self updateSearchResultsForSearchController:self.searchController];
}
@end
 
在SearchResultTableViewController.m文件中
#import "SearchResultTableViewController.h"
#import "Product.h"
@interface SearchResultTableViewController ()
@end
@implementation SearchResultTableViewController
- (void)viewDidLoad {
    [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;
}
@end

iOS搜索框的更多相关文章

  1. iOS --- 搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  2. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  3. iOS搜索框UISearchBar

    当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...

  4. iOS搜索框UISearchBar 分类: ios技术 2015-04-03 08:55 82人阅读 评论(0) 收藏

    当你在seachBar中输入字母之前的时候,只是用鼠标选中searchBar的时候,如图 终端输出截图如下:(这个时候调用先shouldBeginEditing,之后调用didBeginEditing ...

  5. IOS搜索框输入中文解决方案(防抖)

    class Header extends React.Component { constructor(props) { super(props); this.time = 0; // 重点在于这个th ...

  6. iOS之搜索框UISearchController的使用(iOS8.0以后替代UISearchBar+display)

    在iOS 8.0以上版本中, 我们可以使用UISearchController来非常方便地在UITableView中添加搜索框. 而在之前版本中, 我们还是必须使用UISearchBar + UISe ...

  7. iOS开发——UI篇OC篇&TextField作为搜索框的使用

    TextField作为搜索框的使用 在iOS开发中我们经常会使用到搜索框,但是有的时候系统自带的搜索框不足以满足我吗想要的功能,这个时候我们就可以使用自定义的搜索框实现想要的功能. 今天就简单的介绍一 ...

  8. 【好程序员笔记分享】——iOS开发之使用TextField作为搜索框

    -iOS培训,iOS学习-------型技术博客.期待与您交流!------------ iOS开发之使用TextField作为搜索框     今天给大家带来一个新的技巧,比如平时我们要使用代码创建一 ...

  9. ios UISearchBar搜索框的基本使用

    摘要: 小巧简洁的原生搜索框,漂亮而易用,如果我们的应用没有特殊需求,都可以使用它. iOS中UISearchBar(搜索框)使用总结 初始化:UISearchBar继承于UIView,我们可以像创建 ...

随机推荐

  1. 【bzoj3233】【ahoi2013】找硬币

    题意: 求确定n种货币面额x1..xn满足 x1=1 且xi为xj的整数倍(i>j) 给定n个物品价格ai 求使用上面货币最少需要硬币数(不能找零) 题解: 动态规划 听说网上的题解都是搜索的做 ...

  2. 依赖包bcrypt安装Issues

    说明:本文在个人博客地址为edwardesire.com,欢迎前来品尝. 在决策树项目中,使用到了bcrypt依赖包来加密文件.在wini8(win7)部署安装这个依赖的时候容易出现出现了问题. 解决 ...

  3. webSocket vnc rfb

  4. Nginx的代理和反向代理

    什么是代理? 代理是为网络用户代理了来访问网络的,比如Google agent代理FQ. 什么是反向代理? 以代理服务器来接受internet上的连接请求,然后将请求转发给内部网络上的服务器,并将从服 ...

  5. poj 1847 Tram

    http://poj.org/problem?id=1847 这道题题意不太容易理解,n个车站,起点a,终点b:问从起点到终点需要转换开关的最少次数 开始的那个点不需要转换开关 数据: 3 2 1// ...

  6. [iOS基础控件 - 6.9.2] 静态单元格 QQ功能列表

    使用storyboard设计静态的表格数据   A.实现步骤 1.控制器继承UITableViewController 2.在storyboard中使用TableViewController,删除原来 ...

  7. Serializable 序列化为文件

    package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  8. UI进阶 KVO

    KVO:(Key-Value-Observer)键值观察者,是观察者设计模式的一种具体实现 KVO触发机制:一个对象(观察者),监测另一对象(被观察者)的某属性是否发生变化,若被监测的属性发生的更改, ...

  9. C# App.config文件的使用

    App.config文件 1. 配置文件概述: 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序 ...

  10. ags注册

    在电脑里找到2个 ESRIRegAsm.exe C:\Program Files (x86)\Common Files\ArcGIS\bin C:\Program Files\Common Files ...