iOS 下拉列表

来源:互联网 发布:sql中建立表的命令是 编辑:程序博客网 时间:2024/06/06 05:13


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=[superinitWithFrame:frame];

    

    if(self){

        showlist=NO;

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

        self.tv.delegate =self;

        self.tv.dataSource =self;

        self.tv.backgroundColor = [UIColorgrayColor];

        self.tv.separatorColor = [UIColorlightGrayColor];

        self.tv.hidden =YES;

        [self addSubview:self.tv];

        

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

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

        [self.textFeildaddTarget:selfaction:@selector(dropdown)forControlEvents:UIControlEventAllTouchEvents];

        [self addSubview:self.textFeild];

    }

    return self;

}


-(void)dropdown{

    

    [self.textFeildresignFirstResponder];

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

        return;

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

        

        CGRect sf = self.frame;

        sf.size.height =frameHeight;

        

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

        [self.superviewbringSubviewToFront:self];

        self.tv.hidden =NO;

        showlist = YES;//显示下拉框

        

        CGRect frame =self.tv.frame;

        frame.size.height =0;

        self.tv.frame = frame;

        frame.size.height =tabHeight;

        [UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];

        [UIViewsetAnimationCurve:UIViewAnimationCurveLinear];

        self.frame = sf;

        self.tv.frame = frame;

        [UIViewcommitAnimations];

    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return 1;

}


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

{

    return [self.tableArraycount];

}


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

{

    static NSString *CellIdentifier =@"Cell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {

        cell = [[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier];

    }

    

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

    cell.textLabel.font = [UIFontsystemFontOfSize: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.tableArrayobjectAtIndex:[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 = [[DropDownalloc] initWithFrame:CGRectMake(10,10, 140, 100)];

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

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

    dd1.tableArray = arr;

    [self.viewaddSubview:dd1];






0 0
原创粉丝点击