下面再贴一下自己的关键代码
1,设置代理,UISearchBarDelegate,UISearchDisplayDelegate
<span style="font-size:12px;">@interface FKRSearchBarTableViewController () <UITableViewDataSource, UITableViewDelegate,
UISearchBarDelegate, UISearchDisplayDelegate> {
NSMutableArray * resultItems;
NSMutableArray * suggestItems;
}
@property(nonatomic, strong, readwrite) UISearchBar *searchBar;
@property(nonatomic, strong) UISearchDisplayController *strongSearchDisplayController;</span>
2,ViewDidLoad方法 初始化
- (void)viewDidLoad
{
[super viewDidLoad];
resultItems = [[NSMutableArray alloc] initWithCapacity:20];
suggestItems = [[NSMutableArray alloc] initWithCapacity:20];
//1,初始化searchbar
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.placeholder = @"Search";
self.searchBar.delegate = self;
[_searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone]; //默认是句子首字母大写,这里设置为none
[self.searchBar sizeToFit];
//2,初始化tableview,并将searbar设置为tableview的tableHeaderView
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.tableHeaderView=_searchBar;
[self.view addSubview:_tableView];
//3.初始化SearchDisplayController
self.strongSearchDisplayController = [[UISearchDisplayController alloc]
initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
//4.设置搜索栏默认数据,即全部联系人
[self fetchItems];
}
3,代理方法设置
(1)代理方法1:当search文本内容改变的时候调用
业务处理如下:当文本内容长度大于零,根据文本内容搜索,否则查询所有。代码如下:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([self.searchBar.text length] > 0) {
[self doSearch];
} else {
[self fetchMembers];
[self setionWithFriends];
[self.tableView reloadData];
}
}
(2)代理方法2:点击取消按钮时候调用
业务如下:点击取消按钮的时候,重置响应者,清空搜索文本内容,tableview加载全部联系人,代码如下:
<span style="font-size:12px;">- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//重置响应者
[self.searchBar resignFirstResponder];
// 清空搜索文本
self.searchBar.text = @"";
// 隐藏取消按钮
self.searchBar.showsCancelButton = NO;
// 加载默认数据
[self fetchMembers];
[self setionWithFriends];
[self.tableView reloadData];
}</span>
(3)代理方法3:文本开始编辑的时候调用,显示取消按钮,代码如下:
<span style="font-size:12px;">- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.searchBar.showsCancelButton = YES;
}</span>
4,tableview最常用的三个代理方法的设置
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
注:判断tableview 是否是searchDisplayController的searchResultsTableView
<span style="font-size:12px;"> if(tableView == self.searchDisplayController.searchResultsTableView){
}else{
}</span>
最后给大家看下又长又臭的代码,我已经不忍直视了。
<span style="font-size:14px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == self.searchDisplayController.searchResultsTableView){
return 1;
}else{
if (self.showSectionIndexes) {
return self.sections.count;
} else {
return 1;
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//if (section<1)return 1;
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return [suggestItems count];
}else{
//分组排序
if (self.showSectionIndexes) {
return [[self.sections objectAtIndex:section] count];
//显示全部
} else {
return self.friendArray.count;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFKRSearchBarTableViewControllerDefaultTableViewCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kFKRSearchBarTableViewControllerDefaultTableViewCellIdentifier];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
for(UIView * elem in [cell.contentView subviews])
{
if([elem isKindOfClass:[BDSuggestLabel class]])
{
NSLog(@"remove");
[elem removeFromSuperview];
}
}
BDSuggestLabel * richTextLabel = [[BDSuggestLabel alloc] initWithFrame:CGRectMake(10, 10, 300, 25)];
richTextLabel.text = [suggestItems objectAtIndex:indexPath.row];
richTextLabel.keyWord = self.searchBar.text;//设置当前搜索的关键字
richTextLabel.backgroundColor = [UIColor clearColor];
richTextLabel.font = [UIFont systemFontOfSize:17.0f];
richTextLabel.textColor = [UIColor grayColor];
[cell.contentView addSubview:richTextLabel];
} else {
//显示分组
if (self.showSectionIndexes) {
if (indexPath.section<1) {
//cell.textLabel.text=@"xxoo";
AddressFirstCell *cell=[[[NSBundle mainBundle]loadNibNamed:@"AddressFirstCell" owner:self options:nil]lastObject];
cell.blok=^(int button){
switch (button) {
case 0:
[self.navigationController pushViewController:self.colleague animated:YES];
break;
case 1:
[self.navigationController pushViewController:self.dept animated:YES];
break;
case 2:
[self.navigationController pushViewController:self.common animated:YES];
break;
default:
break;
}
};
return cell;
}else{
//cell.textLabel.text = [[self.sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
JAMember *member= [[self.sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text=member.name;
DLog(@"%@",cell.textLabel.text);
if([member.memberDetail.image length]>0){
NSData * imageData = [NSData dataWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:member.memberDetail.image]];
cell.imageView.image=[UIImage imageWithData:imageData];
}
}
//显示全部
} else {
cell.textLabel.text = [self.friendArray objectAtIndex:indexPath.row];
}
}
return cell;
}</span>
第一次写博客,目的是记录自己的学习经历,大神请轻喷,又原意一起交流的可以加个好友,后续我会把代码再整理下,再贴出源代码
下面再贴一下自己的关键代码
1,设置代理,UISearchBarDelegate,UISearchDisplayDelegate
<span style="font-size:12px;">@interface FKRSearchBarTableViewController () <UITableViewDataSource, UITableViewDelegate,
UISearchBarDelegate, UISearchDisplayDelegate> {
NSMutableArray * resultItems;
NSMutableArray * suggestItems;
}
@property(nonatomic, strong, readwrite) UISearchBar *searchBar;
@property(nonatomic, strong) UISearchDisplayController *strongSearchDisplayController;</span>
2,ViewDidLoad方法 初始化
- (void)viewDidLoad
{
[super viewDidLoad];
resultItems = [[NSMutableArray alloc] initWithCapacity:20];
suggestItems = [[NSMutableArray alloc] initWithCapacity:20];
//1,初始化searchbar
self.searchBar = [[UISearchBar alloc] init];
self.searchBar.placeholder = @"Search";
self.searchBar.delegate = self;
[_searchBar setAutocapitalizationType:UITextAutocapitalizationTypeNone]; //默认是句子首字母大写,这里设置为none
[self.searchBar sizeToFit];
//2,初始化tableview,并将searbar设置为tableview的tableHeaderView
self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
self.tableView.dataSource = self;
self.tableView.delegate = self;
self.tableView.tableHeaderView=_searchBar;
[self.view addSubview:_tableView];
//3.初始化SearchDisplayController
self.strongSearchDisplayController = [[UISearchDisplayController alloc]
initWithSearchBar:self.searchBar contentsController:self];
self.searchDisplayController.searchResultsDataSource = self;
self.searchDisplayController.searchResultsDelegate = self;
self.searchDisplayController.delegate = self;
//4.设置搜索栏默认数据,即全部联系人
[self fetchItems];
}
3,代理方法设置
(1)代理方法1:当search文本内容改变的时候调用
业务处理如下:当文本内容长度大于零,根据文本内容搜索,否则查询所有。代码如下:
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {
if ([self.searchBar.text length] > 0) {
[self doSearch];
} else {
[self fetchMembers];
[self setionWithFriends];
[self.tableView reloadData];
}
}
(2)代理方法2:点击取消按钮时候调用
业务如下:点击取消按钮的时候,重置响应者,清空搜索文本内容,tableview加载全部联系人,代码如下:
<span style="font-size:12px;">- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
//重置响应者
[self.searchBar resignFirstResponder];
// 清空搜索文本
self.searchBar.text = @"";
// 隐藏取消按钮
self.searchBar.showsCancelButton = NO;
// 加载默认数据
[self fetchMembers];
[self setionWithFriends];
[self.tableView reloadData];
}</span>
(3)代理方法3:文本开始编辑的时候调用,显示取消按钮,代码如下:
<span style="font-size:12px;">- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
self.searchBar.showsCancelButton = YES;
}</span>
4,tableview最常用的三个代理方法的设置
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
注:判断tableview 是否是searchDisplayController的searchResultsTableView
<span style="font-size:12px;"> if(tableView == self.searchDisplayController.searchResultsTableView){
}else{
}</span>
最后给大家看下又长又臭的代码,我已经不忍直视了。
<span style="font-size:14px;">- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
if(tableView == self.searchDisplayController.searchResultsTableView){
return 1;
}else{
if (self.showSectionIndexes) {
return self.sections.count;
} else {
return 1;
}
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//if (section<1)return 1;
if(tableView == self.searchDisplayController.searchResultsTableView)
{
return [suggestItems count];
}else{
//分组排序
if (self.showSectionIndexes) {
return [[self.sections objectAtIndex:section] count];
//显示全部
} else {
return self.friendArray.count;
}
}
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kFKRSearchBarTableViewControllerDefaultTableViewCellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kFKRSearchBarTableViewControllerDefaultTableViewCellIdentifier];
}
if(tableView == self.searchDisplayController.searchResultsTableView){
for(UIView * elem in [cell.contentView subviews])
{
if([elem isKindOfClass:[BDSuggestLabel class]])
{
NSLog(@"remove");
[elem removeFromSuperview];
}
}
BDSuggestLabel * richTextLabel = [[BDSuggestLabel alloc] initWithFrame:CGRectMake(10, 10, 300, 25)];
richTextLabel.text = [suggestItems objectAtIndex:indexPath.row];
richTextLabel.keyWord = self.searchBar.text;//设置当前搜索的关键字
richTextLabel.backgroundColor = [UIColor clearColor];
richTextLabel.font = [UIFont systemFontOfSize:17.0f];
richTextLabel.textColor = [UIColor grayColor];
[cell.contentView addSubview:richTextLabel];
} else {
//显示分组
if (self.showSectionIndexes) {
if (indexPath.section<1) {
//cell.textLabel.text=@"xxoo";
AddressFirstCell *cell=[[[NSBundle mainBundle]loadNibNamed:@"AddressFirstCell" owner:self options:nil]lastObject];
cell.blok=^(int button){
switch (button) {
case 0:
[self.navigationController pushViewController:self.colleague animated:YES];
break;
case 1:
[self.navigationController pushViewController:self.dept animated:YES];
break;
case 2:
[self.navigationController pushViewController:self.common animated:YES];
break;
default:
break;
}
};
return cell;
}else{
//cell.textLabel.text = [[self.sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
JAMember *member= [[self.sections objectAtIndex:indexPath.section] objectAtIndex:indexPath.row];
cell.textLabel.text=member.name;
DLog(@"%@",cell.textLabel.text);
if([member.memberDetail.image length]>0){
NSData * imageData = [NSData dataWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:member.memberDetail.image]];
cell.imageView.image=[UIImage imageWithData:imageData];
}
}
//显示全部
} else {
cell.textLabel.text = [self.friendArray objectAtIndex:indexPath.row];
}
}
return cell;
}</span>
第一次写博客,目的是记录自己的学习经历,大神请轻喷,又原意一起交流的可以加个好友,后续我会把代码再整理下,再贴出源代码