您的位置:首页 > 移动开发 > 微信开发

类似微信的通讯录搜索

2017-01-11 21:15 316 查看
一,在当前界面实现搜索功能

使用UISearchController ,在.m文件中调用以下方法

1.设置属性

@interface FriendViewController ()<UITableViewDelegate,UITableViewDataSource,UISearchResultsUpdating,UISearchControllerDelegate>
{
UITableView *_tableView;
NSMutableArray *_dataSource;
NSMutableArray * _searchArray;//搜索结果的数组
UISearchController * _searchVC;//搜索视图
UITableViewController * _searchTableView;//搜索结果的表格视图
}
@end


2,方法的调取

- (void)viewDidLoad {
[super viewDidLoad];

[self createTableView];
[self createSearchController];
[self allAppRequestWithParm];
}


3,创建表格视图

#pragma mark 搜索视图
- (void)createSearchController
{
//表格界面 UITableViewController表格视图控制器 tableview是表格视图
_searchTableView = [[UITableViewController alloc]initWithStyle:UITableViewStylePlain];
_searchTableView.tableView.dataSource = self;
_searchTableView.tableView.delegate = self;
_searchTableView.tableView.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
_searchTableView.tableView.placeHeader=_noneView;

//创建搜索界面
_searchVC = [[UISearchController alloc]initWithSearchResultsController:_searchTableView];
_searchVC.delegate=self;
//把表格视图控制器跟搜索界面相关联(防止searchBar发生64像素的偏移量)
self.definesPresentationContext = YES;

//在tableView存在右侧索引情况下防止搜索框右侧缺少一块
UIView *headerView = [[UIView alloc] init];
headerView.frame = CGRectMake(0, 0, ScreenWidth, 44);
[headerView addSubview:_searchVC.searchBar];
_tableView.tableHeaderView = headerView;
_searchVC.searchResultsUpdater = self;

//修改searchBar的属性
UIView *subView =_searchVC.searchBar.subviews[0];
for (UIView *view in subView.subviews)
{
if ([view isKindOfClass:NSClassFromString(@"UISearchBarBackground")])
{
[view removeFromSuperview];
//修改搜索框背景
UIView *backView = [[UIView alloc] init];
backView.backgroundColor = H_CellContenViewBackColor;
backView.frame = CGRectMake(0, -20, ScreenWidth, 64);
[_searchVC.searchBar insertSubview:backView atIndex:0];
}
//自定义textField
else if ([view isKindOfClass:[UITextField class]])
{
UITextField *textField = (UITextField *)view;
textField.layer.borderWidth = 0.5;
textField.layer.borderColor = H_LINECOLOR.CGColor;
textField.layer.cornerRadius = 2;
textField.clipsToBounds = YES;
}
}
}


实现搜索框搜索时候的代理方法

#pragma mark 搜索的协议方法
- (void)updateSearchResultsForSearchController:(UISearchController *)searchController
{
//这句代码实现cell顶部和searchBar不发生重叠
self.edgesForExtendedLayout = UIRectEdgeAll;
//在点击搜索时会调用一次,点击取消按钮又调用一次
//判断当前搜索是否在搜索状态还是取消状态
if (_searchVC.isActive) {
if (searchController.searchBar.text.length!=0) {
[self searchAllDateRequestWithDict:dict];
//表示搜索状态
//调取搜索的接口
}
}
}


修改searchBar在搜索状态时候右侧取消按钮的属性

#pragma mark -- UISearchControllerDelegate
- (void)willPresentSearchController:(UISearchController *)searchController{
[UIView animateWithDuration:0.1 animations:^{
//修改取消按钮字体颜色
searchController.searchBar.showsCancelButton = YES;
UIButton *cancelBtn=[searchController.searchBar valueForKey:@"cancelButton"];
[cancelBtn setTitleColor:H_GREENCOLOR forState:UIControlStateNormal];
}];
}


#pragma mark -- 创建列表
- (void)createTableView
{
_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavTabHeight) style:UITableViewStylePlain];
_tableView.delegate = self;
_tableView.dataSource = self;
[_tableView setSeparatorColor:H_LINECOLOR];
[self.view addSubview:_tableView];
}

#pragma mark -- tableView的delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
if (tableView == _tableView) {
return _dataSource.count;
}
else{
return _searchArray.count;
}
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == _tableView) {
static NSString *identify = @"friendListCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identify];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;

FriendGroupModel *model = _dataSource[indexPath.row];
cell.textLabel.text = model.group;
cell.textLabel.font = H_FONTCONTENTNAME;
cell.textLabel.textColor = H_BLACKTEXTCOLOR;
[image mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.equalTo (cell.mas_top).mas_offset((50-11)/2);
make.right.equalTo (cell.mas_right).mas_offset(-15);
make.width.mas_equalTo(6);
make.height.mas_equalTo(11);
}];
return cell;
}
else{
FriendListCell *cell = [FriendListCell createCellWithTableView:tableView];
cell.listModel = _searchArray[indexPath.row];
return cell;
}
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
if (tableView == _tableView) {
return 50;
}
else{
return 60;
}
}
#pragma mark -- 请求数据
- (void)allAppRequestWithParm
{
//调取接口
//刷新数据
[weakself allAppRequestWithParm];
}

#pragma mark -- 通讯录搜索接口
- (void)searchAllDateRequestWithDict:(NSDictionary *)dict
{
//调取接口
//刷新搜索界面的tableview
[_searchTableView.tableView reloadData];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: