import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchBarDelegate,UISearchResultsUpdating>
@property (strong,nonatomic) NSMutableArray *dataList;
@property (strong,nonatomic) NSMutableArray *searchList;
@property (nonatomic, strong) UISearchController *searchController;
@end
@implementation ViewController
{ UITableView * _tableView; } - (void)viewDidLoad { [super viewDidLoad]; _dataList = [NSMutableArray array]; _searchList = [NSMutableArray array]; _tableView = [[UITableView alloc] initWithFrame:CGRectMake(, , self.view.frame.size.width, self.view.frame.size.height - ) style:UITableViewStylePlain]; _tableView.delegate = self; _tableView.dataSource = self; [self.view addSubview:_tableView]; _searchController = [[UISearchController alloc] initWithSearchResultsController:nil]; _searchController.searchResultsUpdater = self; _searchController.dimsBackgroundDuringPresentation = NO; _searchController.hidesNavigationBarDuringPresentation = NO; _searchController.searchBar.frame = CGRectMake(self.searchController.searchBar.frame.origin.x, self.searchController.searchBar.frame.origin.y, self.searchController.searchBar.frame.size.width, 44.0); _tableView.tableHeaderView = self.searchController.searchBar; self.dataList=[NSMutableArray arrayWithCapacity:]; for (NSInteger i=; i<; i++) { [self.dataList addObject:[NSString stringWithFormat:@"%ld-FlyElephant",(long)i]]; } } //设置区域 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return ; } //设置区域的行数 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (self.searchController.active) { return [self.searchList count]; }else{ return [self.dataList count]; } } //返回单元格内容 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ static NSString *flag=@"cellFlag"; UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:flag]; if (cell==nil) { cell=[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:flag]; } if (self.searchController.active) { [cell.textLabel setText:self.searchList[indexPath.row]]; } else{ [cell.textLabel setText:self.dataList[indexPath.row]]; } return cell; } -(void)updateSearchResultsForSearchController:(UISearchController *)searchController { NSString *searchString = [self.searchController.searchBar text]; NSPredicate *preicate = [NSPredicate predicateWithFormat:@"SELF CONTAINS[c] %@", searchString]; if (self.searchList!= nil) { [self.searchList removeAllObjects]; } //过滤数据 self.searchList= [NSMutableArray arrayWithArray:[_dataList filteredArrayUsingPredicate:preicate]]; //刷新表格 [_tableView reloadData]; }- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. }

iOS-UISearchController用法的更多相关文章

  1. iOS UISearchController 的使用方法

    iOS UISearchController 的使用方法 UISearchController 让用户在 UISearchBar 上输入搜索关键词,展示搜索结果或者进行其他操作.UISearchCon ...

  2. IOS NSInvocation用法简介

    IOS NSInvocation用法简介 2012-10-25 19:59 来源:博客园 作者:csj007523 字号:T|T [摘要]在 iOS中可以直接调用某个对象的消息方式有两种,其中一种就是 ...

  3. iOS - UISearchController

    前言 NS_CLASS_DEPRECATED_IOS(3_0, 8_0, "UISearchDisplayController has been replaced with UISearch ...

  4. iOS UISearchController的使用

    在iOS9中,UISearchDisplayController 已经被UISearchController替代.搜索框是一种常用的控件. 假设我们要满足下图的需求,产生100个“数字+三个随机字母” ...

  5. IOS UIButton用法详解

    这段代码动态的创建了一个UIButton,并且把相关常用的属性都列举了.希望对大家有用.   //这里创建一个圆角矩形的按钮UIButton *button1 = [UIButton buttonWi ...

  6. iOS - NSError用法规范

    iphone跬步之--错误信息 NSError   一.获取系统的错误信息 比如移动文件时,获取文件操作错误: NSError *e = nil;[[NSFileManager defaultMana ...

  7. ios 系统参数用法

    qi前言:写一个宏来选择性地编译与运行为不同iOS所写的代码来支持多个版本的ios工程 #if __IPHONE_OS_VERSION_MIN_REQUIRED #import "xxxxx ...

  8. iOS Block 用法 (1)-once again

    Block简介: Block的实际行为和Function很像,最大的差别是在可以存取同一个Scope的变量值.Block实体形式如下: ^(传入参数列){行为主体}; Block实体开头是“^”,接着 ...

  9. IOS UIWebView用法

    转自猫猫小屋 IOS webview控件使用简介(一) IOS webview控件使用简介(二)–加载本地html

  10. iOS TextField用法大全

    //初始化textfield并设置位置及大小 UITextField *text = [[UITextField alloc]initWithFrame:CGRectMake(20, 20, 130, ...

随机推荐

  1. 「PHP」工厂方法模式

    引言   所属:创建型模式,常用设计模式之一 工厂模式分为:简单工厂模式.工厂方法模式.静态工厂模式.抽象工厂模式. 下面为工厂方法模式. 参考资料: <大话设计模式>程杰   模式概述 ...

  2. java读写HDFS

    package cn.test.hdfs;   import java.io.IOException; import java.net.URI; import java.net.URISyntaxEx ...

  3. Java核心技术36讲----------谈谈final、finally、finalize有什么不同

    一.final 1.final修饰方法时,需要注意的点: #final修饰方法时,之前的第二个原因是效率.但是如果方法过于庞大,可能看不到内嵌调用带来的任何性能提升.在最近的Java版本中,不需要使用 ...

  4. 树莓派编译程序时报错:virtual memory exhausted: Cannot allocate memory

    一.原因分析: 树莓派内存太小,编译程序会出现virtual memory exhausted: Cannot allocate memory的问题,可以用swap扩展内存的方法. 二.解决方法: 安 ...

  5. AMD,CMD,CommonJs,UMD讲解

    一.CommonJS CommonJS规范加载模块是同步的,只有加载完成,才能执行后面的操作 CommonJS规范中的module.exports和require 每个文件就是一个模块,有自己的作用域 ...

  6. Java基础之this和super关键字用法

    this 在java中,this是一个引用当前类对象的引用变量,主要有以下几种用法: 引用当前类的实例变量· this关键字可以用来引用当前类的实例变量.如果实例变量和某个方法的参数之间存在歧义(实例 ...

  7. cakephp1.3中help form的一个小问题

    如果我们在模版里这么干 <?php echo $form->input('last_sold_date',array('autocomplete'=>'off','label'=&g ...

  8. bbblack的网络socket通信实验

    1. 本次用bbblack作网络的通信实验,对了,这个板子必须装SD卡才能启动吗?板载的4GB eMMC Flash 存储器,eMMC (Embedded Multi Media Card) 为MMC ...

  9. 新买的 SSD 固态硬盘竟然是坏的,我傻了啊!

    1. 今天早上上班路上在网上下单了一个 1 T 的 SSD 固态硬盘,晚上 7 点半左右送到手后迫不及待想替换掉原来的机械硬盘,在这个新硬盘上装系统,玩起来. 2. 拆开包装,先用移动硬盘接口检查下新 ...

  10. 在nginx环境下,直接用域名访问(首页)

    ①: server { listen 80; server_name www.njm1.com; location = / { #=/规则可以直接访问域名.如:www.njm1.com.跳转到http ...