UISearchBar和UISearchDisplayController
原文 http://hi.baidu.com/happywilma0118/item/e6d5730a499bba1b3a53eef8
UISearchBar继承自UIView、UIResponder、NSObject
属性:
autocapitalizationType————自动对输入文本对象进行大小写设置(包含4种类型,但是有时候键盘会屏蔽此属性)
autocorrectionType————自动对输入文本对象进行纠错。
backgroundImage————searchbar的背景图片,如果图片不是可伸缩的或者1点宽,则一般被平铺
barStyle————控件的样式
delegate————控件的委托,委托要遵从UISearchBarDelegate协议,默认是nil
keyboardType————输入时,键盘的样式
placeholder————半透明的提示文字,输入搜索内容消失
prompt————显示在控件顶部的一行提示文字
text————控件上面的显示的文字
showsBookmarkButton————是否在控件的右端显示一个书的按钮(输入文字时消失)
showsCancelButton————是否显示cancel按钮(默认是显示)
showsSearchResultsButton————是否在控件的右端显示搜索结果按钮
searchResultsButtonSelected————搜索结果按钮是否被选中
tintColor————bar的颜色(具有渐变效果)
translucent————指定控件是否会有透视效果
scopeButtonTitles————搜索栏下部的选择栏,数组里面的内容是按钮的标题
selectedScopeButtonIndex————搜索栏下部的选择栏按钮的个数
showsScopeBar————控制搜索栏下部的选择栏是否显示出来(需设置为YES 才能使用scopebar)
UISearchBar不执行搜索行为,必须使用delegate,当输入搜索文本、点击button按钮后,代理的方法会完成搜索对应的操作。
1.编辑输入事件:
2.点击按钮事件:
3.Scope按钮事件:
利用UISearchBar的委托事件 textDidChange, 当在搜索框中输入完成后,如果输入的文本长度>0,可以调用自己的搜索方法,得到搜索结果,然后再reloadData,刷新一下。如果输入文本长 度<0,则需要恢复到原始数据。这个方法可以在边输入搜索文本边显示结果。 如果需要按“search”按钮再搜索,则将上述操作放在searchBarSearchButtonClicked中。
利用UISearchDisplayController可以简化很多操作,也能达到搜索的目的。
属性:
active————是搜索界面可视化,默认为no,可用setActive方法设置.
delegate————委托
searchBar————在searchdisplaycontroller初始化后,searchbar是不可修改的,是readonly属性的.
searchContentController————管理搜索内容的试图控制器,一般是一个UITableViewController的实例,意思是针对一个UITableView的内容进行搜索
searchResultsDataSource————搜索结果的数据源
searchResultsDelegate————搜索结果的委托
searchResultsTableView————搜索结果要展示在哪个tableview中(read-only);
searchResultsTitle————搜索结果视图的title
初始化一个searchDisplayController:
UISearchBar * theSearchBar = [[[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width-50, 40)] autorelease];
theSearchBar.placeholder = @"enter province name";
theSearchBar.autocorrectionType = UITextAutocorrectionTypeNo;
theSearchBar.autocapitalizationType = UITextAutocapitalizationTypeAllCharacters;
theSearchBar.scopeButtonTitles = [NSArray arrayWithObjects:@"All",@"A",@"B",@"C",@"D" ,nil];
theSearchBar.showsScopeBar = YES;
theSearchBar.keyboardType = UIKeyboardTypeNamePhonePad;
theSearchBar.showsBookmarkButton = YES;
tableView.tableHeaderView = theSearchBar; //将searchBar添加到tableView的头,注意滚动出屏幕后,搜索框也不在了,只出现在首页
UISearchDisplayController * searchdispalyCtrl = [[UISearchDisplayController alloc] initWithSearchBar:theSearchBar contentsController:self];
searchdispalyCtrl.active = NO;
searchdispalyCtrl.delegate = self;
searchdispalyCtrl.searchResultsDelegate=self;
searchdispalyCtrl.searchResultsDataSource = self;
使用UISearchDisplayDelegate的委托方法进行搜索操作:
1.搜索状态改变:
2.装载和卸载tableview:
3.显示和隐藏tableview:
4.搜索条件改变时响应:
searchDisplayController 自身有一个searchResultsTableView,所以在执行操作的时候首先要判断是否是搜索结果的tableView,如果是显示的就是搜索结 果的数据,如果不是,是TableView自身的view,则需要显示原始数据。
if(tableView == self.searchDisplayController.searchResultsTableView)
{
arr = [self.filterContent valueForKey:key]; //搜索结果
}
else
{
arr = [self.localresource valueForKey:key]; //原始数据
}
这样就不需要每次都realoadData了。
一 个很好的实例可以参考:http://developer.apple.com/library/ios/#samplecode /TableSearch/Introduction/Intro.html#//apple_ref/doc/uid/DTS40007848
UISearchBar和UISearchDisplayController的更多相关文章
- 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS
那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...
- UISearchBar和 UISearchDisplayController的使用
感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...
- iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- iOS开发之直接使用UISearchBar
iOS开发中经常需要使用SearchBar,我们可以选择使用UISearchBar+UISearchController或者UISearchBar+UISearchDisplayController( ...
- iOS 用UISearchDisplayController实现查找功能
UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用 示例代码如下: // // WKRootViewController ...
- iOS UISearchDisplayController学习笔记
UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示.UISearchDisplayController提供了显示搜索结果的 ...
- UISearchController替换UISearchDisplayController
随着iOS 的升级,iOS 7的占有率更低了.Xcode 升级到Xcode 8之后,对iOS 应用支持的最低版本,iOS 7也被抛弃了.我在新项目中也是最低支持到iOS 8,因此工程里也是各种警告.首 ...
- iOS开发-搜索栏UISearchBar和UISearchController
iOS中UISearchDisplayController用于搜索,搜索栏的重要性我们就不说了,狼厂就是靠搜索起家的,现在越来越像一匹没有节操的狼,UC浏览器搜索栏现在默认自家的神马搜索,现在不管是社 ...
- [转] iOS开发-搜索栏UISearchBar和UISearchController
原文网址: http://www.cnblogs.com/xiaofeixiang/p/4273620.html?utm_source=tuicool iOS中UISearchDisplayContr ...
随机推荐
- linux中vim中文显示乱码
这里所说的都是全局设定,打开vimrc文件后,只需要在文件最后添加以下代码就可以了: set fileencodings=utf-8,gb2312,gbk,gb18030 set termencodi ...
- Just Cause系列游戏品鉴
没错, 这又是一个游戏点评, 因为实在没地写了, 想起来我还欠JC系列许多售后评价, 就专门写了这篇blog来总结下JC系列的特色, 以及它最新的游戏引擎apex, JC4月初的时候发布的, 虽然和3 ...
- 清除DataGridView显示的数据
一.DataGridView未绑定数据时清空数据 this.dgv_PropDemo.DataSource = null 二.DataGridView绑定数据时清空数据 DataGridView绑定了 ...
- apt-get强制使用Ipv4
sudo apt-get -o Acquire::ForceIPv4=true update 永久解决办法: 创建文件 /etc/apt/apt.conf.d/99force-ipv4 加入代码: A ...
- e659. 缩放,剪取,移动,翻转图形
AffineTransform tx = new AffineTransform(); tx.scale(scalex, scaley); tx.shear(shiftx, shifty); tx.t ...
- linux -- 进程管理和作业控制
一. 作业控制 1. 直接将命令放到后台"执行": & [root @test /root ]# command & 范例: [root @test /root] ...
- 修改Java标准库源码
以下是摘抄,实际操作没有测试 先前我曾提到,原本想借由“改动Java标准库源码”来测知Class object的生成,但由于其ctor原始设计为private,也就是说不可能透过这个管道生成Cla ...
- Supervision 行为模式
官方链接:http://erlang.org/doc/man/supervisor.html http://erlang.org/doc/design_principles/sup_princ.htm ...
- 关于对afx_msg的解释-----来源百度百科
1AFX前缀 Afx前缀是微软MFC一个小组的名称简写,并没有别的意义. MFC的很多代码,包括全局函数名.宏.头文件名都使用了"Afx". Afx*.h是一组MFC的核心头文件, ...
- HttpHelper万能框架GetMergeCookie的问题
用万能框架写了一个DZ带验证码POST登录一直错误 http://www.sufeinet.com/thread-17795-1-1.html 调试半天发现是框架GetMergeCookie的问题,, ...