@interface ViewController () <UITableViewDelegate,UITableViewDataSource> {
}
@property (nonatomic,weak)UITableView *listView;
@property (nonatomic,strong)NSArray *arrays;
@property (nonatomic,strong) UILabel *label1;
@property (nonatomic,strong) UILabel *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;
}
}