下拉列表

来源:互联网 发布:linuxmint优化 编辑:程序博客网 时间:2024/05/17 20:33

#import <UIKit/UIKit.h>


@interface DownList :UIView<UITableViewDataSource,UITableViewDelegate>

{

    BOOL _isShow;

}

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSArray *dataArray;

@property(nonatomic,strong)UIButton *button;

-(instancetype)initWithFrame:(CGRect)frame andWithDataArray:(NSArray *)dataArray;

@end



#import "DownList.h"


@implementation DownList

-(instancetype)initWithFrame:(CGRect)frame andWithDataArray:(NSArray *)dataArray{

    if (self = [superinitWithFrame:frame]) {

        self.dataArray = dataArray;

        [self createView:self.dataArray[0]];

    }

    return self;

}

-(void)createView:(NSString*)str{

    _isShow = NO;

    //创建button

    self.button = [UIButtonbuttonWithType:UIButtonTypeCustom];

    self.button.frame =CGRectMake(0,0, self.frame.size.width,30);

     [self.buttonaddTarget:selfaction:@selector(dropDown)forControlEvents:UIControlEventAllEvents];

    [self.buttonsetTitle:str forState:UIControlStateNormal];

    [self.buttonsetTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

    self.button.backgroundColor= [UIColoryellowColor];

    [self addSubview:self.button];

    //创建tableView

    self.tableView = [[UITableViewalloc]initWithFrame:CGRectMake(0,30, self.frame.size.width,0) style:UITableViewStyleGrouped];

    self.tableView.dataSource =self;

    self.tableView.delegate =self;

    self.tableView.backgroundColor = [UIColorgrayColor];

    self.tableView.separatorColor = [UIColorlightGrayColor];

    self.tableView.hidden =YES;

    [self addSubview:self.tableView];

}


-(void)dropDown{

    if (!_isShow) {

        [UIViewanimateWithDuration:0.5animations:^{

            CGRect sf =self.frame;

            sf.size.height =200;

            self.frame = sf;

            CGRect frame = self.tableView.frame;

            frame.size.height =150;

            self.tableView.frame = frame;

            self.tableView.hidden =NO;

        }];

    }

    else{

        [UIViewanimateWithDuration:0.5animations:^{

            CGRect sf =self.frame;

            sf.size.height =30;

            self.frame = sf;


            CGRect frame = self.tableView.frame;

            frame.size.height =0;

            self.tableView.frame = frame;

            self.tableView.hidden =YES;

        }];

    }

}


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

    return self.dataArray.count;

}

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

    return 50;

}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{

    return 0.1;

}


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

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:@"cell"];

    if (cell == nil) {

        cell = [[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:@"cell"];

    }

    

    cell.textLabel.text =self.dataArray[indexPath.row];

    return cell;

    

}


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

    [self.buttonsetTitle:self.dataArray[indexPath.row]forState:UIControlStateNormal];

    CGRect frame = self.tableView.frame;

    frame.size.height =0;

    self.tableView.frame = frame;

    self.tableView.hidden =YES;

    _isShow = NO;

    CGRect sf =self.frame;

    sf.size.height =30;

    self.frame = sf;

}



0 0