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

UISearchBar套接TableView实现搜索功能

2015-07-22 09:50 639 查看
1.方法

本工程中使用导航栏,把SearchBar作为UITableView1(用于存放搜索选择结果)的头视图,



在点击SearchBar的时候,隐藏导航栏,在UITableView1上面添加一个覆盖层,



开始搜索时,再添加一个UITableView2覆盖在最上层显示搜索结果



2.代码实现

#import "SearchViewController.h"

#import "SearchTableViewCell.h"

@interface
SearchViewController () <UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource,ASIHttpRequestDelegate>{
   
UISearchBar *_searchBar;
   
UITableView *_resultTableView; //搜索选择结果表格在最下层
   
UITableView *_searchTableView; //搜索表格视图在上层
   
UIButton *_envelopV
4000
iewBtn;

    
   
NSMutableArray *_searchMutableArray; //搜索结果数组
   
NSMutableArray *_emailsChooseMutableArray; //用于保存选择员工的邮箱,用于创建日程
   
BOOL _isSearchTableViewAdd; //是否添加了搜索表格视图

    
   
UIView *_statusBarView;
   
UIView *_loadingView; //加载框

    
   
ASIHttpRequest *_httpRequest; //统一网络请求封装
   
NSUInteger _currentCount; //当前数量
}

@end

@implementation SearchViewController

//@synthesize resultMutableArray = self.resultMutableArray;

- (void)viewWillAppear:(BOOL)animated {
    [superviewWillAppear:animated];

    [_resultTableViewreloadData];
}

- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    self.navigationItem.title =@"人员搜索";

    self.navigationController.navigationBar.backgroundColor
= [UIColor blueColor];

    
   
UIBarButtonItem *rightBarButtonItem = [[UIBarButtonItemalloc]initWithTitle:@"确定"style:UIBarButtonItemStyleDonetarget:selfaction:@selector(rightBarButtonItemAction)];

    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

    

    

    //初始化数组

//    self.resultMutableArray = [[NSMutableArray alloc] initWithObjects:nil];

    _searchMutableArray = [[NSMutableArrayalloc]initWithObjects:nil];

    _emailsChooseMutableArray = [[NSMutableArrayalloc]initWithObjects:nil];

    

    //初始化网络请求对象

    _httpRequest = [[ASIHttpRequestalloc]init];

    _httpRequest.delegate =self;

    

    _currentCount =0;

    _isSearchTableViewAdd =NO;

    

    _searchBar = [[UISearchBaralloc]initWithFrame:CGRectMake(0,0,APPW,44)];

    _searchBar.placeholder =@"请输入工号/姓名/手机号";

    _searchBar.barStyle =UIBarMetricsDefault;

    _searchBar.tintColor = [UIColorblueColor];

    _searchBar.translucent =YES;

    _searchBar.delegate =self;

    

    //搜索选择结果视图,从搜索表格过来

    _resultTableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,0,APPW,APPH
-44)];

    _resultTableView.delegate =self;

    _resultTableView.dataSource =self;

    _resultTableView.tag =0;

    

    _resultTableView.tableHeaderView =_searchBar;

    [self.viewaddSubview:_resultTableView];

    

    _envelopViewBtn = [[UIButtonalloc]initWithFrame:CGRectMake(0,44,APPW,
APPH -44)];

    _envelopViewBtn.backgroundColor = [UIColorblackColor];

    _envelopViewBtn.alpha =0.0f;

    [_envelopViewBtnaddTarget:selfaction:@selector(clickEnvelopBtnAction:)forControlEvents:UIControlEventTouchUpInside];

    

    //搜索表格从网络获取数据
   
_searchTableView = [[UITableView
alloc] initWithFrame:CGRectMake(0,64,APPW,
APPH -64 -
48)];

    _searchTableView.delegate =self;

    _searchTableView.dataSource =self;

    _searchTableView.tag =1;

    

    //初始化

    for (StaffModel *staffModelinself.resultMutableArray)
{

        [_emailsChooseMutableArrayaddObject:staffModel.email];
    }
}

- (void)rightBarButtonItemAction {
   
//向调用此组件的控制器传值
    //...

    [self.navigationControllerpopViewControllerAnimated:YES];
}

/*

 遮盖层响应事件

 */
- (void)clickEnvelopBtnAction:(UIButton *)button {

    [selfenvelopViewControl:0];
}

/*

 遮盖层视图控制

 用于添加遮盖层或者移除遮盖层

 */
- (void)envelopViewControl:(float)alpha {

    [UIViewanimateWithDuration:0.05animations:^{
       
_envelopViewBtn.alpha = alpha;
    }completion:^(BOOL finished) {
       
if (alpha <= 0) {
//移除遮盖层

            [_envelopViewBtnremoveFromSuperview];

            [_searchBarsetShowsCancelButton:NOanimated:YES];

            [_searchBarresignFirstResponder];

            [self.navigationControllersetNavigationBarHidden:NOanimated:YES];
        }
else { //添加遮盖层

            [_resultTableViewaddSubview:_envelopViewBtn];

            [_resultTableViewbringSubviewToFront:_envelopViewBtn];
        }
    }];
}

#pragma mark UISearchBarDelegate
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {

    [_searchBarsetShowsCancelButton:YESanimated:YES];

    [self.navigationControllersetNavigationBarHidden:YESanimated:YES];

    [selfenvelopViewControl:0.3];

    //设置searchBar开始编辑时的状态栏
    //...

}

- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar {

    [_statusBarViewremoveFromSuperview];
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {

    [_statusBarViewremoveFromSuperview];

    [_searchTableViewremoveFromSuperview];

    [_searchMutableArrayremoveAllObjects];

    [_searchTableViewreloadData];

    [selfenvelopViewControl:0];

    

    _isSearchTableViewAdd =NO;
    searchBar.text =nil;

    [searchBar resignFirstResponder];

    [_resultTableViewreloadData];
}

- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText {

    [_httpRequestclearDelegatesAndCancel];

    _httpRequest.delegate =self;

    [_loadingViewremoveFromSuperview];
   
if (searchBar.text.length >0) {

        if (!_isSearchTableViewAdd) {

            _isSearchTableViewAdd =YES;
            [self.viewaddSubview:_searchTableView];//添加UITableView2,接收服务器返回的值
        }

        [selfstaffSearchRequest];
    }
else {    
        //移除UITableView2,并刷新,刷新UITableView1

        _isSearchTableViewAdd =NO;

        [_searchMutableArrayremoveAllObjects];

        [_searchTableViewremoveFromSuperview];

        [_searchTableViewreloadData];

        [_resultTableViewreloadData];
    }
}

#pragma mark UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
   
if (tableView.tag ==
0) {

        returnself.resultMutableArray.count;
    }
else {

        return_searchMutableArray.count;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath
*)indexPath {

    
   
static NSString *cellId =
@"searchStaff";
   
SearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
   
//自定义cell样式赋值 
    //...
   
return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
 
//点击cell响应事件
}

#pragma mark UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
return 70;
}

- (void)staffSearchRequest {
   //网络数据请求

    //...

    _loadingView = [LampLoadinitWithForView:self.viewwithLabel:@"查询中"width:100tag:1001];
}

#pragma mark DPMHttpRequest

//统一封装请求成功
- (void)requestFinished:(ASIHTTPRequest *)request {

    [_loadingViewremoveFromSuperview];
   //解析数据服务器返回的数据
    //...
}

//统一封装请求失败
- (void)requestFailed:(ASIHTTPRequest *)request {

    [_loadingViewremoveFromSuperview];
   
NSLog(@"请求失败");
}

@end

GitHub源码下载: https://github.com/hengyizhangcn/SearchBarDemo.git
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uitableview UISearchBar