UITableView 上滑动 隐藏搜索框,下滑,显示搜索框



//
//  AuthSelectDoctorVC.m
//  com.yx129.yxClientDoctor3
//
//  Created by yx on 15/1/4.
//  Copyright (c) 2015年 Guangzhou Yixiang Internet Technology Development Limited. All rights reserved.
//

#define SearchNumber  15

#import "AuthSelectDoctorCell.h"
#import "AuthSelectDoctorVC.h"

@interface AuthSelectDoctorVC ()<UITableViewDataSource,UITableViewDelegate,ASIHTTPRequestDelegate,MJRefreshBaseViewDelegate,UIAlertViewDelegate,UITextFieldDelegate>
{
//    UIImageView *_tipView;
//    UILabel *_tipLabel;
    
    YXTableView *_tableView;
    
    NSMutableArray *_doctorsArray;
    
    MJRefreshFooterView *_footerView;
    UIAlertView *_alertView;
    NSString *_authoriseDoctorId;
    
    UITextField *_searchTF;
    NSString *_changeStr;
    NSString *_oldStr;
    
    UIView *_parentView;
    
}
@end

@implementation AuthSelectDoctorVC

-(id)init
{
    if (self = [super init]) {
        _doctorsArray = [NSMutableArray array];
        _curr_page = 1;
        _page_count = SearchNumber;
    }
    return self;
}

-(void)onClickLeftBtn
{
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [_navView setTitle:@"选择授权医生" type:NavTypeBackOnly];
    self.view.backgroundColor = kViewBackColor;
    
    [self prepareView];
    [self loadDoctorWithKeyword:nil AndIsSearchType:NO AndPage:1];
}



-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:YES];
    
    [_footerView free];
}


-(void)refreshData
{
    [_doctorsArray removeAllObjects];
    [self loadDoctorWithKeyword:_oldStr AndIsSearchType:YES AndPage:1];
}


-(void)prepareView
{
    _parentView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_navView.frame), IPHONE_WIDTH, IPHONE_HEIGHT)];
    _parentView.backgroundColor = kViewBackColor;
    [self.view addSubview:_parentView];
    
    CGFloat width = IPHONE_WIDTH;
    CGFloat height = kTableViewCellHeight;
    CGFloat y = 0; //CGRectGetMaxY(_navView.frame);
    
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, y, IPHONE_WIDTH, height)];
    bgView.backgroundColor = [UIColor whiteColor];
    [_parentView addSubview:bgView];
    
    width  -= 30;
    height = height*3/5;
    UIView *borderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width,height )];
    borderView.backgroundColor = kViewBackColor;
    borderView.center = CGPointMake(bgView.center.x,CGRectGetHeight(bgView.frame)/2); //bgView.center;
    borderView.layer.borderWidth = 0.8;
    borderView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    borderView.layer.cornerRadius = height/2;
    [bgView addSubview:borderView];
    
    width -= 30;
    height -= 10;

    _searchTF = [[UITextField alloc] initWithFrame:CGRectMake(20, 5, width, height)];
    _searchTF.borderStyle = UITextBorderStyleNone;
    _searchTF.center = CGPointMake(CGRectGetWidth(borderView.frame)/2, CGRectGetHeight(borderView.frame)/2);
    //    _searchTF.tintColor = [UIColor grayColor];
    _searchTF.font = kTitleFont;
    _searchTF.backgroundColor = [UIColor clearColor];
    _searchTF.placeholder = @"搜索医生名";
    _searchTF.delegate = self;
    _searchTF.returnKeyType = UIReturnKeySearch;
    _searchTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [borderView addSubview:_searchTF];
    
    if (SystemVersion < 7.0) {
        _searchTF.center = CGPointMake(_searchTF.center.x, _searchTF.center.y + 7);
    }
    
    /*
    UIImage *image = [UIImage imageNamed:@"n_no_data"];
    _tipView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, kSizePix(image.size.width) , kSizePix(image.size.height))];
    _tipView.center = CGPointMake(self.view.center.x, CGRectGetMaxY(borderView.frame) + CGRectGetHeight(_tipView.frame)/2 + 60);  // self.view.center;
    
    _tipView.userInteractionEnabled = YES;
    [self.view addSubview:_tipView];
    
    _tipLabel = [[UILabel alloc] initWithFrame:CGRectZero];
    _tipLabel.backgroundColor = [UIColor clearColor];
    _tipLabel.text = @"没有查到相关医生";
    _tipLabel.font = kTitleFont;
    [_tipLabel sizeToFit];
    _tipLabel.center = CGPointMake(self.view.center.x, CGRectGetMaxY(_tipView.frame) + 40);
    [self.view addSubview:_tipLabel];
    */
    
    _tableView =[[YXTableView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(bgView.frame)+kTopInsect, IPHONE_WIDTH, kContentFrame.size.height - kTableViewCellHeight - kTopInsect) style:UITableViewStylePlain];
    _tableView.backgroundColor = kViewBackColor;
    _tableView.dataSource=self;
    _tableView.delegate=self;
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    [_parentView addSubview:_tableView];
    
    _footerView = [[MJRefreshFooterView alloc] initWithScrollView:_tableView];
    _footerView.delegate = self;
    
//    UISwipeGestureRecognizer *swipeGesture =[[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeGestureRecognizerAction:)];
//    [_tableView addGestureRecognizer:swipeGesture];
    
    if (SystemVersion >= 7.0)
        
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(textChangedOld)
                                                     name:UITextFieldTextDidChangeNotification
                                                   object:_searchTF];
    //    else
    //
    //        [[NSNotificationCenter defaultCenter] addObserver:self
    //                                                 selector:@selector(textChanged)
    //                                                     name:UITextFieldTextDidChangeNotification
    //                                                   object:_searchTF];
    
    [self.view bringSubviewToFront:_navView];
}

-(void)textChangedOld
{
    BOOL isEqual = [_searchTF.text isEqualToString:_changeStr];
    
    if ( isEqual || ((!isEqual) && (_oldStr.length > _searchTF.text.length))) {
        _oldStr = _searchTF.text;
        
        [_doctorsArray removeAllObjects];
        [self loadDoctorWithKeyword:_searchTF.text AndIsSearchType:YES AndPage:1];
    }
    
    _changeStr = _searchTF.text;
    
}

-(void)textChanged
{
    [_doctorsArray removeAllObjects];
    
    [self loadDoctorWithKeyword:_searchTF.text AndIsSearchType:YES AndPage:1];
}


-(void)loadDoctorWithKeyword:(NSString *)keyword AndIsSearchType:(BOOL)isSearch AndPage:(int)page
{
    _curr_page = page;

    [self.hudManager With_label:@"请稍后..." duration:10];
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
    
    if ([self.asiRequest isExecuting]) {
        return;
    }
    
    NSURL * url = [NSURL URLWithString:entranceURL];
    self.asiRequest = [[ASIFormDataRequest alloc] initWithURL:url];
    [self.asiRequest setDelegate:self];
    [self.asiRequest setDidFinishSelector:@selector(requestDidFinished:)];
    [self.asiRequest setDidFailSelector:@selector(requestDidFailed:)];
    [self.asiRequest addPostValue:@"Sourceclient" forKey:@"module"];
    [self.asiRequest addPostValue:@"Doctor" forKey:@"controller"];
    [self.asiRequest addPostValue:@"getDoctorList" forKey:@"action"];
    [self.asiRequest addPostValue:kSecret forKey:@"secret"];
    [self.asiRequest addPostValue:kSharedDoctor.token_key forKey:@"token_key"];
    [self.asiRequest addPostValue:kSharedDoctor.acc_id forKey:@"acc_id"];
    [self.asiRequest setPostValue:@"0" forKey:@"is_search"];
    
    [self.asiRequest addPostValue:[NSNumber numberWithInt:page] forKey:@"curr_page"];
    [self.asiRequest addPostValue:[NSNumber numberWithInt:SearchNumber] forKey:@"page_count"];
    
    
    if (isSearch) {
        [self.asiRequest setPostValue:keyword forKey:@"search_key"];
        [self.asiRequest setPostValue:@"1" forKey:@"is_search"];
    }
    
    [self.asiRequest setRequestMethod:@"POST"];
    [self.asiRequest setTimeOutSeconds:10];
    [self.asiRequest startAsynchronous];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



//数据请求成功
-(void)requestDidFinished:(ASIFormDataRequest *)request
{
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [self.hudManager hide];
    
    NSDictionary *objDic = [NSJSONSerialization JSONObjectWithData:request.responseData options:NSJSONReadingMutableContainers error:nil];
    
    int code = [[objDic objectForKey:@"code"] intValue];
    NSString *msg = [objDic objectForKey:@"msg"];
    NSArray *listArray = [objDic objectForKey:@"list"];
    
    [MBProgressHUDManager showText:msg];
    
    //数据请求成功
    if (40000 == code && (![listArray isKindOfClass:[NSNull class]])){
        
        for (NSDictionary *dic in [objDic objectForKey:@"list"]) {
            
            DoctorModel *doctor = [[DoctorModel alloc] init];
            doctor.phone = [dic objectForKey:@"doctor_accid"];
            doctor.acc_id = [dic objectForKey:@"doctor_id"];
            doctor.doctor_name = [dic objectForKey:@"doctor_name"];
            doctor.doctor_portrait = [dic objectForKey:@"doctor_ portrait"];
            doctor.jobtitle_list = [dic objectForKey:@"jobtitle_name_list"];
            doctor.dept_list = [dic objectForKey:@"dep_name_list"];
            
            [_doctorsArray addObject:doctor];
        }
        
//        _tableView.hidden = NO;
        [_tableView hideTipView];
        _curr_page++;
        
        _footerView.hidden = listArray.count < SearchNumber? YES:NO;
        
    }else{
        
//        _tipView.image = [UIImage imageNamed:@"icon_no_data"];
//        _tipLabel.text = @"没有相关数据";
        
//        _tableView.hidden = YES;
        
        [_tableView showNoDataTipViewWithMessage:@"没有相关数据"];
        
        _footerView.hidden = YES;
    }
    
    [_tableView reloadData];
    
}


-(void)requestDidFailed:(ASIFormDataRequest *)request
{
    [self.hudManager hide];
    //关闭网络
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
    [MBProgressHUDManager showText:@"请求失败,请稍后再试..."];
    
    NSData *responseData = [request responseData];
    NSString* returnString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
    [_tableView showEorrorTipViewWithMessage:@"加载失败"];
    
//    _tableView.hidden = YES;
//    _tipView.image = [UIImage imageNamed:@"icon_no_network"];
//    _tipLabel.text = @"没有网络";
    
}

#pragma mark - UITextFieldDelegate

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    _oldStr = textField.text;
    [_searchTF resignFirstResponder];
    [_doctorsArray removeAllObjects];
    [self loadDoctorWithKeyword:_searchTF.text AndIsSearchType:YES AndPage:1];
    
    return YES;
}


#pragma mark - UITableViewDelegate


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _doctorsArray.count;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kTableViewCellHeight;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellID = @"cellID";
    AuthSelectDoctorCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    
    if (!cell) {
        cell = [[AuthSelectDoctorCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        cell.authoriseBtnClicked = ^(NSString *docID){
            _authoriseDoctorId = docID;
            //NSString *message = [NSString stringWithFormat:@"是否授权%@医生",]
            _alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否授权" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
            [_alertView show];
            
        };
        
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:cell.bounds];
        imgView.image = [UIImage imageNamed:@"cellBgWhite"];
        imgView.contentMode = UIViewContentModeScaleToFill;
        cell.backgroundView = imgView;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
    }
    
    if (_doctorsArray.count > indexPath.row) {
        
        DoctorModel *model = [_doctorsArray objectAtIndex:indexPath.row];
        [cell setDoctorModel:model];
    }
    return cell;
}


#pragma mark - MJRefreshDelegate

-(void)refreshViewBeginRefreshing:(MJRefreshBaseView *)refreshView
{
    
    if (_curr_page <= 10){
        
        [self loadDoctorWithKeyword:_oldStr AndIsSearchType:YES AndPage:_curr_page];
    }
    [refreshView endRefreshing];
}

#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    
    if (buttonIndex == 1 && _authoriseDoctorId) {
        
        [self addAuthoriseAction:_authoriseDoctorId];
        
    }
}

-(void)addAuthoriseAction:(NSString *)doctorID
{
    NSMutableDictionary *postDic =[[NSMutableDictionary alloc] init];
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    [postDic setObject:@"Sourceclient" forKey:@"module"];
    [postDic setObject:@"Authorise" forKey:@"controller"];
    [postDic setObject:@"AuthoriseManage" forKey:@"action"];    //diseaseList,doctor
    [postDic setObject:kSecret forKey:@"secret"];
    [postDic setObject:kSharedDoctor.acc_id forKey:@"acc_id"];
    [postDic setObject:kSharedDoctor.token_key forKey:@"token_key"];
    [postDic setObject:doctorID forKey:@"doctor_id"];
    [postDic setObject:@"1" forKey:@"type"];
    
    
    YXHttpRequest * request = [[YXHttpRequest alloc]initWithPath:entranceURL params:postDic requestType:kTypePost completion:^(NSDictionary *result, NSError * error){
        
        //[HUDManager hide];
        [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
        
        if (!error) {
            
            NSString *msg = [result objectForKey:@"msg"];
            [MBProgressHUDManager showText:msg];
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }else{
            [request showErrorMessage];
        }
    }];
    
    
}


-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];

    float height = [_tableView numberOfRowsInSection:0]*kTableViewCellHeight;
    
    if (height <= CGRectGetMaxY(self.view.frame) - kTableViewCellHeight - kTopInsect) {
        return;
    }
    
    float contentOffsetY = scrollView.contentOffset.y;
    
    if (contentOffsetY > 0) {
        
        if (CGRectGetMinY(_parentView.frame) >= CGRectGetMaxY(_navView.frame)) {
            _tableView.bounces = NO;
            
            CGRect frame = _tableView.frame;
            frame.size.height += kTableViewCellHeight+kTopInsect;
            _tableView.frame = frame;
            
            [UIView animateWithDuration:1 animations:^{
               
                _parentView.center = CGPointMake(_parentView.center.x, _parentView.center.y - kTableViewCellHeight - kTopInsect);
                
                
            } completion:^(BOOL finished) {
                
                
            }];
        }

    }else if(contentOffsetY < 0){
    
        _tableView.bounces = NO;
        
        if (CGRectGetMinY(_parentView.frame) < CGRectGetMaxY(_navView.frame)) {
            
            [UIView animateWithDuration:1 animations:^{
                
                _parentView.center = CGPointMake(_parentView.center.x, _parentView.center.y + kTableViewCellHeight + kTopInsect);
               
                
            } completion:^(BOOL finished) {
                
                CGRect frame = _tableView.frame;
                frame.size.height -= kTableViewCellHeight+kTopInsect;
                _tableView.frame = frame;
            }];
        }
        
    }
    
    _tableView.bounces = YES;


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];

}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

                              

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值