ios学习--iphone 实现下拉菜单

时间:2021-02-13 11:12:54

http://blog.163.com/ytrtfhj@126/blog/static/8905310920116224445195/

 

通过网上资料,自己借助资料写的代码,这是完全代码


//****************************************************************************


@interface DropDown1 :UIView <UITableViewDelegate,UITableViewDataSource> {

   UITableView *tv;//下拉列表

    NSArray *tableArray;//下拉列表数据

    UITextField *textField;//文本输入框

    BOOL showList;//是否弹出下拉列表

    CGFloat tabheight;//table下拉列表的高度

    CGFloat frameHeight;//frame的高度

}


@property (nonatomic,retain)UITableView *tv;

@property (nonatomic,retain)NSArray *tableArray;

@property (nonatomic,retain)UITextField *textField;


@end


//****************************************************************************


@implementation DropDown1


@synthesize tv,tableArray,textField;


- (void)dealloc

{

    [tvrelease];

    [tableArrayrelease];

    [textFieldrelease];

    [superdealloc];

}


-(id)initWithFrame:(CGRect)frame

{

    if (frame.size.height<200) {

        frameHeight =200;

    }else{

        frameHeight = frame.size.height;

    }

   tabheight = frameHeight-30;

    

    frame.size.height =30.0f;

    

    self=[superinitWithFrame:frame];


    if(self){

        showList =NO; //默认不显示下拉框

        

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

        tv.delegate =self;

        tv.dataSource =self;  

       tv.backgroundColor = [UIColorgrayColor];  

       tv.separatorColor = [UIColorlightGrayColor];  

        tv.hidden =YES;  

        [selfaddSubview:tv];  


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

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

        [textFieldaddTarget:selfaction:@selector(dropdown)forControlEvents:UIControlEventAllTouchEvents];

        [selfaddSubview:textField];

        

    }

   return self;

}

-(void)dropdown{

    [textFieldresignFirstResponder];

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

        return;

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

        

        CGRect sf =self.frame;

        sf.size.height =frameHeight;

        

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

        [self.superviewbringSubviewToFront:self];

        tv.hidden =NO;

        showList =YES;//显示下拉框

        

        CGRect frame =tv.frame;

        frame.size.height =0;

        tv.frame = frame;

        frame.size.height =tabheight;

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

        [UIViewsetAnimationCurve:UIViewAnimationCurveLinear];  

        self.frame = sf;

        tv.frame = frame;

        [UIViewcommitAnimations];

    }

}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

    return1;

}


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

{

   return [tableArraycount];

}


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

{

    staticNSString *CellIdentifier = @"Cell";

    

    UITableViewCell *cell = [tableViewdequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell ==nil) {

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

    }

    

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

    cell.textLabel.font = [UIFontsystemFontOfSize:16.0f];

    cell.accessoryType =UITableViewCellAccessoryNone;

    cell.selectionStyle =UITableViewCellSelectionStyleGray;

    

    return cell;

}

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

{

    return35;

}

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

{

    textField.text = [tableArrayobjectAtIndex:[indexPath row]];

    showList =NO;

    tv.hidden =YES;

    

    CGRect sf = self.frame;
    sf.size.height = 30;
    self.frame = sf;

    CGRect frame = tv.frame;
    frame.size.height = 0;
    tv.frame = frame;

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

   // Return YES for supported orientations

    return (interfaceOrientation ==UIInterfaceOrientationPortrait);

}


@end


//****************************************************************************


上面的是实现方法,下面是使用:


 DropDown1 *dd1 = [[DropDown1alloc] initWithFrame:CGRectMake(10,10, 140, 100)];

 dd1.textField.placeholder =@"请输入联系方式";

 NSArray* arr=[[NSArrayalloc]initWithObjects:@"电话",@"email",@"手机",@"aaa",@"bbb",@"ccc",nil];

 dd1.tableArray = arr;

 [arr release];

 [self.viewaddSubview:dd1];

 [dd1 release];