UITableView 如何实现搜索框功能
2012-06-06 09:58
483 查看
from:http://blog.csdn.net/tangaowen/article/details/6527427
UITableView 如何实现搜索框功能
1。定义如下两个变量,并且声明为属性。
UISearchBar * searchBar;
UISearchDisplayController * searchDc;
2。在loadView中,初始化 这两个控件
[cpp] view
plaincopy
- (void)loadView
{
//在这里创建搜索栏和搜索显示控制器
self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
self.searchBar.tintColor=[UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f];
self.searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
self.searchBar.keyboardType=UIKeyboardTypeAlphabet;
self.searchBar.hidden=NO;
self.searchBar.placeholder=[NSString stringWithCString:"请输入需要查找的文本内容" encoding: NSUTF8StringEncoding];
self.tableView_.tableHeaderView=self.searchBar;
self.searchDc=[[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] autorelease];
self.searchDc.searchResultsDataSource=self;
self.searchDc.searchResultsDelegate=self;
[self.searchDc setActive:NO];
}
3。在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
函数中,对搜索控制器做特殊化处理。
[cpp] view
plaincopy
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==tableView_)
{
//如果是普通的TableView
//返回正常的cell个数
}
else
{
//如果是搜索显示控制器
std::string strToFind=[self.searchBar.text UTF8String];
//然后,根据strToFind去筛选 符合搜索条件的结果,并且
//返回符合条件的cell的个数。
}
}
4。在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
中,根据 是否是搜索控制器,返回不同的cell 数据。
5。在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
等等需要特殊处理的函数中,都加上对搜索控制器的 特殊处理。
UITableView 如何实现搜索框功能
1。定义如下两个变量,并且声明为属性。
UISearchBar * searchBar;
UISearchDisplayController * searchDc;
2。在loadView中,初始化 这两个控件
[cpp] view
plaincopy
- (void)loadView
{
//在这里创建搜索栏和搜索显示控制器
self.searchBar=[[UISearchBar alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 44.0f)];
self.searchBar.tintColor=[UIColor colorWithRed:0.8f green:0.8f blue:0.8f alpha:1.0f];
self.searchBar.autocorrectionType=UITextAutocorrectionTypeNo;
self.searchBar.autocapitalizationType=UITextAutocapitalizationTypeNone;
self.searchBar.keyboardType=UIKeyboardTypeAlphabet;
self.searchBar.hidden=NO;
self.searchBar.placeholder=[NSString stringWithCString:"请输入需要查找的文本内容" encoding: NSUTF8StringEncoding];
self.tableView_.tableHeaderView=self.searchBar;
self.searchDc=[[[UISearchDisplayController alloc] initWithSearchBar:self.searchBar contentsController:self] autorelease];
self.searchDc.searchResultsDataSource=self;
self.searchDc.searchResultsDelegate=self;
[self.searchDc setActive:NO];
}
3。在 - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
函数中,对搜索控制器做特殊化处理。
[cpp] view
plaincopy
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if(tableView==tableView_)
{
//如果是普通的TableView
//返回正常的cell个数
}
else
{
//如果是搜索显示控制器
std::string strToFind=[self.searchBar.text UTF8String];
//然后,根据strToFind去筛选 符合搜索条件的结果,并且
//返回符合条件的cell的个数。
}
}
4。在 - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath
中,根据 是否是搜索控制器,返回不同的cell 数据。
5。在 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
等等需要特殊处理的函数中,都加上对搜索控制器的 特殊处理。
相关文章推荐
- UITableView 如何实现搜索框功能
- sharepoint 2013 在office 365中 如何嵌入JS代码实现搜索功能
- django学习——如何实现简单的搜索功能
- 如何实现简单的自动提示(autocomplete)填充搜索功能java代码。
- lookup第一篇:如何在salesforce增强lookup搜索功能,实现搜索所有可用记录和实现搜索同一对象下的all field
- 动易系统如何实现按频道搜索的功能
- 仿站技巧二十四-织梦(dedecms)如何实现多字段搜索功能?
- Lucene.Net如何实现搜索结果分类统计功能
- ajax 如何实现搜索输入框联想功能
- 【腾讯云搜分享】如何实现搜索智能联想(搜索下拉提示)功能
- 站内搜索关键词功能如何实现
- 如何在Web网站实现搜索功能
- 如何用VC60实现类似windows搜索文件的功能,针对文件夹中包含的文件名进行模糊查
- 手把手教你如何运用强大的谷歌自定义搜索功能来实现你的自定义搜索站之用谷歌自定义搜索实现网盘搜索引擎第一篇
- phpcms 如何实现搜索功能(无分词功能)
- 如何使用ASP.NET Core实现搜索功能
- PHP技术之PHP如何实现SNS网站的功能
- 学术搜索典型场景的软件功能实现分析
- 如何在Dev-C++上实现调试功能
- ASP.NET jQuery 食谱25 (使用jQuery UI的Autocomplete方法实现文本框的自动搜索填充功能)