IOS下拉控件纯代码

#import "ViewController.h"
//tabelView代理
@interface ViewController() 

//定义控件
@property(nonatomic,copy)NSArray *dataArray;
@property(nonatomic,strong)UIButton *button;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)UILabel *selectLabel;
@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    _dataArray = @[@"星期一",@"星期二",@"星期三",@"星期四"];
    
    //添加控件  tableView. 用来显示弹窗
    [self.view addSubview:self.tableView];
    //点击button
    [self.view addSubview:self.button];
    //用label显示控件
    [self.view addSubview:self.selectLabel];
    
}
//初始化tableView。复写初始化方法
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(100, 130, 130, 120) style:UITableViewStylePlain];
    }
    //一定要实现两个代理
    _tableView.delegate = self;
    _tableView.dataSource = self;
    return _tableView;
}
//初始化button。复写button初始化方法
- (UIButton *)button{
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeCustom];
        _button.frame = CGRectMake(70, 100, 30, 30);
        _button.backgroundColor = [UIColor yellowColor];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_button setTitleColor:[UIColor blackColor] forState:UIControlStateSelected];
        [_button setTitle:@"收" forState:UIControlStateNormal];
        [_button setTitle:@"弹" forState:UIControlStateSelected];
        [_button addTarget:self action:@selector(clickToPush:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}
//初始化label
- (UILabel *)selectLabel{
    if (!_selectLabel) {
        _selectLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 130, 30)];
        _selectLabel.text = @" ";
//        _selectLabel.backgroundColor = [UIColor redColor];
        [_selectLabel.layer setCornerRadius:0];
        [_selectLabel.layer setBorderWidth:1];
        [_selectLabel.layer setBorderColor:[UIColor blackColor].CGColor];
    }
    return _selectLabel;
}

//btn中的响应时间 弹出tabelView
- (void)clickToPush:(UIButton *)btn
{
    btn.selected = !btn.selected;
    if (btn.selected == YES) {
        _tableView.frame = CGRectMake(100, 130, 130, 0);
    }
    else {
        _tableView.frame = CGRectMake(100, 130, 130, 120);
    }
}


#pagma mark. tableView Delegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 30;
}



- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return [_dataArray count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    _selectLabel.text = _dataArray[indexPath.section];
    _tableView.frame = CGRectMake(100, 130, 130, 0);
    _button.selected = !_button.selected;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"ID"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ID"];
    }
    cell.backgroundColor = [UIColor orangeColor];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.text = _dataArray[indexPath.section];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

@end

你可能感兴趣的:(IOS)