类似微信的通讯录搜索
2017-01-11 21:15
316 查看
一,在当前界面实现搜索功能
使用UISearchController ,在.m文件中调用以下方法
1.设置属性
2,方法的调取
3,创建表格视图
实现搜索框搜索时候的代理方法
修改searchBar在搜索状态时候右侧取消按钮的属性
使用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]; }
相关文章推荐
- 通讯录搜索排序 类似微信通讯录
- (字母排序搜索)类似微信通讯录效果
- android 类似微信通讯录下面有个显示总联系人的控件怎么加?
- android仿微信通讯录搜索示例(匹配拼音,字母,索引位置)
- RecyclerView利用ItemDecoration实现头部悬停效果【类似微信通讯录效果】
- 类 微信 通讯录,实时搜索,首字母分类,滚动header push,以及右侧字母列表
- 类似微信通讯录界面的排序方法及中文转拼音的方法
- Android类似通讯录模糊匹配搜索实现(号码、首字母、简拼、全拼)
- iOS开发学习-类似微信聊天消息中的电话号码点击保存到通讯录中的功能
- 仿微信通讯录如何得到侧栏索引的搜索字符
- java 实现微信搜索附近人功能 .
- jquery.bigautocomplete.js首次获取焦点触发搜索(类似百度首次的搜索历史)
- BlackBerry 10通讯录上用汉语拼音搜索中文姓名
- 微信小程序初体验,入门练手项目--通讯录,部署上线(二)
- 微信 通讯录 好友列表显示
- Android 仿微信雷达搜索好友
- Linux 下的类似Windows下Everything的搜索工具
- android开发类似微信列表向左滑动按钮操作