您的位置:首页 > 产品设计 > UI/UE

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

等等需要特殊处理的函数中,都加上对搜索控制器的 特殊处理。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: