IOSドロップダウンコントロール純コード

3709 ワード

#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