#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 #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  2. 未能加载文件或程序集“Oracle.DataAccess, Version=2.112.1.0, Culture=neutral, PublicKeyToken=89b483f429c47342”或它的某一个依赖项。 解决方法

    webconfig文件对于oracle的映射错误.需要在以下位置修改 <runtime> <legacyCorruptedStateExceptionsPolicy enabled= ...

  3. Android - 应用名称设置的问题

    今天我想修改我的android应用名称,就是手机桌面上图标下面的名称,根据我的理解我修改AndroidManifest.xml文件中application标签中的android:label=" ...

  4. 观察者模式:关于通知的使用(NSNotificationCenter)

    一.通知的使用方法 1.发出通知 例如:[[NSNotificationCenter defaultCenter]postNotificationName:@"backToFirstPage ...

  5. Spring REST实践之Error Handling

    Error Responses 在REST API中,HTTP状态码有非常重要的作用.API开发者应该坚持为每一个请求返回一个正确合适的状态码,而且应该在响应body中包含有用的.细粒度的错误信息.这 ...

  6. Android Studio @Bind的用法,自动生成findViewById无需再实例化控件

    第一步:app 的build.gradle文件中添加 如下代码: compile 'com.jakewharton:butterknife:7.0.0' 点击Sync Now 同步下载第二步:安装插件 ...

  7. windows无法搜索新更新 80072ee2

      http://windows.microsoft.com/zh-cn/windows/windows-update-error-80072ee2#1TC=windows-7    

  8. zTree实现基本树

    zTree实现基本树 1.实现源代码 <!DOCTYPE html> <html> <head> <title>zTree实现基本树</title ...

  9. jQuery:在一个回调中处理多个请求

    我曾经为Mozilla Developer Network 开发一个新功能,它需要加载一个基本的脚本文件的同时加载一个JSON请求.因为我们使用的是jQuery,意味着要使用 jQuery.getSc ...

  10. C++ Interview - using new and delete to alloc and free memory

    1. dynamic create object and initialization int *pi = new int; // pi points to an uninitialized int ...