- 浏览: 10142 次
文章分类
最新评论
-
sgq0085:
这种公司名一听就不想去
创业维艰之公司名 -
漫步邃思:
主营业务是什么呢,感觉名字有点怪
创业维艰之公司名 -
q315506754:
写的这么好 那个踩是谁干的
程序猿学产品:如何开始一个伟大的idea -
james219:
写的很好,你会成功
程序猿学产品:如何开始一个伟大的idea
类QQ,微信,联系人列表搜索UISearchBar使用
下面再贴一下自己的关键代码
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>
第一次写博客,目的是记录自己的学习经历,大神请轻喷,又原意一起交流的可以加个好友,后续我会把代码再整理下,再贴出源代码
相关推荐
仿写 QQ 微信联系人列表。QQ列表支持联系人 分组 下拉。微信列表支持侧边字母检索。
QQ微信现在可以说是每部智能机安装必备软件了!已经突破2亿用户,覆盖iOS,Andriod、Windows Phone、Symbain、黑莓等...黄天不负有心人哪~终于找到一款无需任何虚拟机就可以安装使用的QQ微信电脑客户端了,分享给大家。
类似微信联系人的listView和类似城市列表的listView
批处理删除QQ和微信聊天记录 强制关闭QQ、微信并删除QQ、微信所有聊天记录和接收的文件 批处理删除QQ和微信聊天记录
仿QQ微信小程序源码.zip
Android 应用,仿照QQ,微信,手机联系人里面的字母导航条的源码
XMPP即时聊天 QQ 微信 用gmail账户登录 无需注册 适用各种版本
QQ微信域名防封处理技术源码 微信网址域名强制跳转至浏览器打开 红域名打开访问 1已被拦截的红域名,通过此源码可以实现不提示拦截,直接在微信内打开。 2无需跳转到浏览器,也能打开 3修复打开失败,跳转卡顿的问题...
一键删除QQ微信记录文件(多路径).bat
全透明QQ微信,唯美风格,让你的QQ与微信跟别人都不一样
介绍腾讯营销QQ微信版新增加功能,微信营销功能展示
关于QQ微信好友列表的Demo,可搜索,滑动,字母的排序,解决排序冲突问题
基于Android的仿QQ微信聊天系统 数据库sqlserver
模仿QQ微信,在列表item左滑删除功能。本demo简单实现了在列表左滑出现删除按钮,点击按钮可删除该item,点击其他地方按钮就隐藏并且实现了惯性滑动。完美解决了滑动冲突和点击冲突。
Android应用源码之仿QQ微信登录页面.zip
基于Android的仿QQ微信聊天系统 (2).zip
封装好的popupwindow类实现qq、qq空间与微信、朋友圈分享
微信QQ防封源码_qq遮罩跳转页面PHP源码美化版 做营销最怕的就是自己的域名被举报,变红?特别是微信好qq,今天分享一套微信QQ防封源码_qq遮罩跳转页面PHP源码美化版 搞一个遮罩(跳转浏览器提示)就OK了
使用方法: 1、上传fanghong目录至网站根目录。 2、将本目录"代码,记得修改!.php"代码全部复制到网站根目录下index.php的开头。 3、复制前请先修改本站名字、链接 替换成 自己的网站名字、链接。 批量替换...
仿QQ 微信 聊天界面 发送表情