#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. js 生成随机数

    <script>   function GetRandomNum(Min,Max){   var Range = Max - Min;   var Rand = Math.random() ...

  2. C# rmi例子

    接口定义 实体定义,注意需要序列化 using System; namespace Interface { [Serializable] public class DataEntity { publi ...

  3. Java(07)转化流

    查看API得知: public class OutputStreamWriter extends Writer public class InputStreamReader extends Reade ...

  4. Codeforces 479E. Riding in a Lift (dp + 前缀和优化)

    题目链接:http://codeforces.com/contest/479/problem/E 题意:         给定一个启示的楼层a,有一个不能去的楼层b,对于你可以去的下一个楼层必须满足你 ...

  5. POJ1338Ugly Numbers(DP)

    http://poj.org/problem?id=1338 第一反应就是DP,DP[i] = min{2*DP[j], 3*DP[k], 5*DP[p] j,k,p<i};于是枚举一下0-i- ...

  6. StructLayout特性

    StructLayout特性          公共语言运行库利用 StructLayoutAttribute控制类或结构的数据字段在托管内存中的物理布局,即类或结构需要按某种方式排列.如果要将类传递 ...

  7. [Oracle]配置path使oracle备份/导入数据命令exp/imp起作用

    将E:\Oracle11g\app\Administrator\product\11.2.0\dbhome_1\bin;路径添加如path

  8. android 动画NineOldAndroid

    NineOldAndroid 1.之前我们用到的第动画是frame和tween动画也就是帧动画,补间动画现在多了一种动画,它包含完了前面动画的所有状态. 属性动画(Property Anmation) ...

  9. [转]Android事件分发机制完全解析,带你从源码的角度彻底理解(上)

    Android事件分发机制 该篇文章出处:http://blog.csdn.net/guolin_blog/article/details/9097463 其实我一直准备写一篇关于Android事件分 ...

  10. JDBC Connection

    [ http://shift-alt-ctrl.iteye.com/blog/1967020]   关于JDBC中关于Connection的两个疑问:   1.Connection实例是线程安全的吗? ...