选中下拉框的位置变化


@interface ViewController () <UITableViewDelegate,UITableViewDataSource> {


}


@property (nonatomic,weak)UITableView *listView;


@property (nonatomic,strong)NSArray *arrays;


@property (nonatomic,strongUILabel *label1;


@property (nonatomic,strongUILabel *label2;


@property (nonatomic,strong)UITapGestureRecognizer *tapLabel;


@end



- (UITableView *)listView{

    

    if (!_listView) {

        

        UITableView *listView = [[UITableViewalloc] init];

        listView.delegate = self;

        listView.dataSource = self;

        listView.backgroundColor = [UIColororangeColor];

        listView.layer.borderWidth =1.0;

        listView.layer.cornerRadius =6.0;

        listView.separatorStyle =UITableViewCellSeparatorStyleNone;

        listView.layer.borderColor = [UIColorblueColor].CGColor;

        [self.viewaddSubview:listView];

        _listView = listView;

    }

    return_listView;

}



- (void)viewDidLoad {

    [superviewDidLoad];

    

    

    self.arrays =@[@"1",@"2",@"3",@"4",@"5"];

//    self.showList = NO;

    

    

    UITapGestureRecognizer *tap = [[UITapGestureRecognizeralloc] init];

    [tap addTarget:selfaction:@selector(tapGes:)];

    [self.coveraddGestureRecognizer:tap];

    

    

    UILabel *label1 = [[UILabelalloc]initWithFrame:CGRectMake(40,200, 160, 20)];

    label1.layer.borderWidth =1;

    label1.layer.cornerRadius =8.0;

    label1.layer.borderColor = [[UIColororangeColor]CGColor];

    label1.userInteractionEnabled =YES;

    [self.viewaddSubview:label1];

    self.label1 = label1;

    

    self.tapLabel = [[UITapGestureRecognizeralloc] init];

    [self.tapLabeladdTarget:selfaction:@selector(tapGes:)];

    [label1 addGestureRecognizer:self.tapLabel];

    label1.tag = 1000;



    UILabel *label2 = [[UILabelalloc]initWithFrame:CGRectMake(40,500, 160, 20)];

    label2.layer.borderWidth =1;

    label2.layer.cornerRadius =6.0;

    label2.layer.borderColor = [[UIColorredColor]CGColor];

    label2.userInteractionEnabled =YES;

    [self.viewaddSubview:label2];

    self.label2 = label2;

    

    self.tapLabel = [[UITapGestureRecognizeralloc] init];

    [self.tapLabeladdTarget:selfaction:@selector(tapGes:)];

    [label2 addGestureRecognizer:self.tapLabel];

    

    label2.tag = 1001;



}


- (void)tapGes:(UITapGestureRecognizer *)tap{

    

    

    self.tapLabel = tap;

    

    CGFloat height = self.arrays.count*20;

    

    CGRect rect = [tap.view.superviewconvertRect:tap.view.frametoView:self.view];

    CGFloat listX = rect.origin.x;

    CGFloat listY = CGRectGetMaxY(rect);

    CGFloat listW = rect.size.width;

    CGFloat listH = height;

    

    CGFloat vcH = self.view.frame.size.height;

    

    

    if (vcH - (listY+listH) >10) {

        

        self.listView.frame =CGRectMake(listX, listY, listW, listH);

        

    }else{

        

        self.listView.frame =CGRectMake(listX, listY-20-listH, listW, listH);


    }

    

    [self.viewaddSubview:self.listView];


    

}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{


    return [self.arrayscount];

}



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

    

    static NSString *listCell =@"listCell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:listCell];

    

    if (cell == nil) {

        

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:listCell];

        

    }

    

    UIView *lineView = [[UIViewalloc]initWithFrame:CGRectMake(0,20, tableView.frame.size.width,1)];

    lineView.backgroundColor = [UIColorblueColor];

    [cell.contentView addSubview:lineView];

    

    cell.textLabel.text = [self.arraysobjectAtIndex:indexPath.row];

    

    return cell;


}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{


    return 20;

    

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{


    NSString *textStr = [self.arraysobjectAtIndex:indexPath.row];

    [tableView removeFromSuperview];


    

    if (self.tapLabel.view.tag ==1000) {

        

        self.label1.text = [NSStringstringWithFormat:@"  %@",textStr];

        

    }else if (self.tapLabel.view.tag ==1001){

        

        self.label2.text = [NSStringstringWithFormat:@"  %@",textStr];

    

    }else{

        

        self.textField1.text = textStr;

    }

    

}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值