原文 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的更多相关文章

  1. 如何在UINavigationBar上添加UISearchBar以及UISearchDisplayController的使用 --OC --iOS

    那我们开始吧,下面是Sely写的一个Demo,分享给大家. 新建一个项目, UISearchDisplayController 的 displaysSearchBarInNavigationBar太死 ...

  2. UISearchBar和 UISearchDisplayController的使用

    感觉好多文章不是很全面,所以本文收集整合了网上的几篇文章,感觉有互相补充的效果. 如果想下载源码来看:http://code4app.com/search/searchbar .本源码与本文无关 1. ...

  3. iOS--- UITableView + UISearchDisplayController - - - - -实现搜索功能

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

  4. iOS开发之直接使用UISearchBar

    iOS开发中经常需要使用SearchBar,我们可以选择使用UISearchBar+UISearchController或者UISearchBar+UISearchDisplayController( ...

  5. iOS 用UISearchDisplayController实现查找功能

    UISearchDisplayController是iOS中用于处理搜索功能的控制器,此控制器需要和UISearchBar结合使用 示例代码如下: // // WKRootViewController ...

  6. iOS UISearchDisplayController学习笔记

    UISearchDisplayController和UISearchBar一起使用用来管理UISearchBar和搜索结果的展示.UISearchDisplayController提供了显示搜索结果的 ...

  7. UISearchController替换UISearchDisplayController

    随着iOS 的升级,iOS 7的占有率更低了.Xcode 升级到Xcode 8之后,对iOS 应用支持的最低版本,iOS 7也被抛弃了.我在新项目中也是最低支持到iOS 8,因此工程里也是各种警告.首 ...

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

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

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

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

随机推荐

  1. JavaScript高级 面向对象(8)--浅拷贝代码实现

    说明(2017.3.31): 1. 浅拷贝,只有值属性,没有引用属性. 2. 在原对象里面添加一个copy方法,返回本对象内的所有值属性. <!DOCTYPE html> <html ...

  2. java安全性的一种简单思路

    关于接口安全性的考虑.这客户端在调用接口时,将acId授权码以加密的方式(可逆加密方式)传递过来, 服务端这边接收后进行解密,然后在服务器端这边的授权名单中进行匹配,判断该授权码是否被授权,从而判断第 ...

  3. 基于java反射的javabean和map相互转换的工具类

    话不多说,代码如下 package com.study; import java.lang.reflect.Field; import java.util.HashMap; import java.u ...

  4. 【WPF】Button按钮添加背景图片

    只是想做一个很简单的图片按钮而已,不需要那么复杂. <Button x:Name="btn" Width="145" Height="30&qu ...

  5. [app]Linux的setitimer和sleep冲突

    在Linux中使用setitimer和sleep会冲突,二者都是用信号 碰上一个头疼的问题,主程序在sleep的时候,总是被开的一个timer的signal callback所影响, 每当timer的 ...

  6. html测试代码框工具

    Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --& ...

  7. C# 属性事件一些设置说明

    大致列举一些常用的属性或事件的一些修饰 用法类似,主要是对属性的进一步设置 [Browsable(true)] public bool Enable {get;set;} 顺便说一下事件的应用: pu ...

  8. BCM_GPIO驱动测试

    在写内核驱动的时候,最好先在uboot上,进行裸板测试,验证寄存器,再移植到内核中,这样可以熟悉寄存器,也排除内核中的一些干扰. /********************************** ...

  9. php对gzip的使用(理论)

    gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式.软件的作者是Jean-loup Gailly和Mark Adler.1992年10月31日第一 ...

  10. PHPExcel正确读取excel表格时间单元格(转载)

    error_reporting(E_ALL); date_default_timezone_set('Asia/shanghai'); /** PHPExcel_IOFactory */ requir ...