#import "ViewController.h"
#import "TestCell.h"
@interface ViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UISearchResultsUpdating>
{
NSMutableArray *dataArray;
NSMutableArray *searchArray;
UISearchController *mySearchController; }
@property (weak, nonatomic) IBOutlet UITableView *myTableView;
@end @implementation ViewController - (void)viewDidLoad
{
[super viewDidLoad]; mySearchController=[[UISearchController alloc]initWithSearchResultsController:nil];
mySearchController.searchResultsUpdater=self;
mySearchController.dimsBackgroundDuringPresentation=NO;
mySearchController.hidesNavigationBarDuringPresentation=NO;
mySearchController.searchBar.frame=CGRectMake(mySearchController.searchBar.frame.origin.x, mySearchController.searchBar.frame.origin.y, mySearchController.searchBar.frame.size.width, 44.0 );
self.myTableView.tableHeaderView=mySearchController.searchBar; dataArray=[NSMutableArray array];
for(NSInteger i=;i<;i++)
{
[dataArray addObject:[NSString stringWithFormat:@"%ld-test",(long)i]];
} } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (mySearchController.active)
{
return searchArray.count;
}
else
{
return dataArray.count;
} }
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return ;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
TestCell *testCell=[tableView dequeueReusableCellWithIdentifier:@"TestCell" forIndexPath:indexPath];
if (mySearchController.active) {
testCell.testLabel.text=searchArray[indexPath.row];
}
else
{
testCell.testLabel.text=dataArray[indexPath.row];
} return testCell;
}
-(void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
NSString *searchString=mySearchController.searchBar.text;
NSPredicate *predicate=[NSPredicate predicateWithFormat:@"self contains[c]%@",searchString];
if (searchArray.count)
{
[searchArray removeAllObjects];
}
searchArray=[NSMutableArray arrayWithArray:[dataArray filteredArrayUsingPredicate:predicate]];
[_myTableView reloadData]; }
@end

iOS-UISearchBar和UISearchController(参考网友来练习一下)的更多相关文章

  1. 《招聘一个靠谱的iOS》面试题参考答案(下)

    相关文章: <招聘一个靠谱的iOS>面试题参考答案(上) 说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外 ...

  2. iOS UISearchBar UISearchController

    搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 交,O2O还是在线教育等都会有一个搜索栏的实现,不过彼此实现效果是 ...

  3. iOS开发-搜索栏UISearchBar和UISearchController

    iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...

  4. [转] iOS开发-搜索栏UISearchBar和UISearchController

    原文网址: http://www.cnblogs.com/xiaofeixiang/p/4273620.html?utm_source=tuicool iOS中UISearchDisplayContr ...

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

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

  6. 招聘一个靠谱的iOS》面试题参考答案(上)

    说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外,其他54道均为简答题. 博文中给出了高质量的面试题,但是未给出答案, ...

  7. 《招聘一个靠谱的iOS》面试题参考答案(上)

    说明:面试题来源是微博@我就叫Sunny怎么了的这篇博文:<招聘一个靠谱的 iOS>,其中共55题,除第一题为纠错题外,其他54道均为简答题. 博文中给出了高质量的面试题,但是未给出答案, ...

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

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

  9. iOS UISearchBar 设置取消按钮,回收键盘,并修改cancel为“取消”

    继承协议: UISearchBarDelegate 在代理方法中设置: #pragma mark --- 搜索框开始编辑 --- - (void)searchBarTextDidBeginEditin ...

随机推荐

  1. Codeforces Round #292 (Div. 1) B. Drazil and Tiles (类似拓扑)

    题目链接:http://codeforces.com/problemset/problem/516/B 一个n*m的方格,'*'不能填.给你很多个1*2的尖括号,问你是否能用唯一填法填满方格. 类似t ...

  2. Odoo constraints 使用教程

    在日常开发Odoo的过程中,我们不免要用到Constraints,中文就是约束. 首先我们来介绍下Odoo里面的两种Constraints. SQL Constraints:就是添加一个数据库的约束. ...

  3. iOS7 各种问题解决

    1 UITableView 行分割线不到头,短线问题 if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) { ...

  4. 【CSDN博客之星】2013年CSDN博客之星正在评选,希望大家支持,非常感谢!

    首先在此感谢 MoreWindows 秒杀多线程面试题系列让我成长和学习,同时也借鉴了很多优秀观点和示例! 请各位读者可以支持MoreWindows,让更优秀的文章陪伴我们! 各位读者好, 本人博客自 ...

  5. javascript js 内存泄露

    JavaScript 内存泄露 1.什么是闭包.以及闭包所涉及的作用域链这里就不说了. 2.JavaScript垃圾回收机制 JavaScript不需要手动地释放内存,它使用一种自动垃圾回收机制(ga ...

  6. Python单步调试

    运行 运行python -m pdb test.py (Pdb) 会自动停在第一行,等待调试,这时你可以看看帮助 (Pdb) h 几个关键命令 断点设置 (Pdb)b 10 #断点设置在本py的第10 ...

  7. extjs表格下的分页条——Ext.grid.Panel 的 pagingtoolbar

    两种分页条:每页固定条数的分页条 和 自定义选择每页内容条数的分页条 一.每页固定条数的分页条 这种样式的-- dockedItems: [{ xtype: 'pagingtoolbar', stor ...

  8. JSON的解析

    一:JSON字符串转换为JSON对象 JSON 最常见的用法之一,是从 web 服务器上读取 JSON 数据(作为文件或作为 HttpRequest),将 JSON 数据转换为 JavaScript ...

  9. 如何修改meclipse中的默认浏览器

    window------->preferrences------------>general-------------->web browser---------->选择你要使 ...

  10. IntervalZero RTX 2014

    2692407267@qq.com,很多其它内容请关注http://user.qzone.qq.com/2692407267 IntervalZero RTX 2014   上图 watermark/ ...