iOS 下拉列表


1.声明  DropDown : UIView

#import <UIKit/UIKit.h>

@interface DropDown : UIView<UITableViewDataSource,UITableViewDelegate>{

    

    BOOL showlist;

    CGFloat tabHeight;

    CGFloat frameHeight; 

}


@property(nonatomic,strong)UITableView *tv;

@property(nonatomic,strong)NSArray *tableArray;

@property(nonatomic,strong)UITextField *textFeild;


@end


#import "DropDown.h"


@implementation DropDown


-(id)initWithFrame:(CGRect)frame{

    

    if(frame.size.height<200){

        frameHeight=200;

        

    }else{

        frameHeight=frame.size.height;

    }

    tabHeight=frameHeight-30;

    

    frame.size.height=30.f;

    

    self=[super initWithFrame:frame];

    

    if(self){

        showlist=NO;

        self.tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 0)];

        self.tv.delegate = self;

        self.tv.dataSource = self;

        self.tv.backgroundColor = [UIColor grayColor];

        self.tv.separatorColor = [UIColor lightGrayColor];

        self.tv.hidden = YES;

        [self addSubview:self.tv];

        

        self.textFeild = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];

        self.textFeild.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格

        [self.textFeild addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];

        [self addSubview:self.textFeild];

    }

    return self;

}


-(void)dropdown{

    

    [self.textFeild resignFirstResponder];

    if (showlist) {//如果下拉框已显示,什么都不做

        return;

    }else {//如果下拉框尚未显示,则进行显示

        

        CGRect sf = self.frame;

        sf.size.height = frameHeight;

        

        //dropdownList放到前面,防止下拉框被别的控件遮住

        [self.superview bringSubviewToFront:self];

        self.tv.hidden = NO;

        showlist = YES;//显示下拉框

        

        CGRect frame =self.tv.frame;

        frame.size.height = 0;

        self.tv.frame = frame;

        frame.size.height = tabHeight;

        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

        [UIView setAnimationCurve:UIViewAnimationCurveLinear];

        self.frame = sf;

        self.tv.frame = frame;

        [UIView commitAnimations];

    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


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

{

    return [self.tableArray count];

}


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

{

    static NSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];

    }

    

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

    cell.textLabel.font = [UIFont systemFontOfSize:16.0f];

    cell.accessoryType = UITableViewCellAccessoryNone;

    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    

    return cell;

}


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

    

    return 35;

}


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

    

    self.textFeild.text = [self.tableArray objectAtIndex:[indexPath row]];

    showlist = NO;

    self.tv.hidden = YES;

    

    CGRect sf = self.frame;

    sf.size.height = 30;

    self.frame = sf;

    CGRect frame = self.tv.frame;

    frame.size.height = 0;

    self.tv.frame = frame;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

    // Return YES for supported orientations

    return (interfaceOrientation == UIInterfaceOrientationPortrait);

}



2.使用

DropDown *dd1 = [[DropDown alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];

    dd1.textFeild.placeholder = @"请输入内容";

    NSArray* arr=[[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",nil];

    dd1.tableArray = arr;

    [self.view addSubview:dd1];






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值