[iPhone开发]UITextField下拉框的实现

#import<UIKit/UIKit.h>

@interfaceDropDownList:UIView<UITableViewDelegate,UITableViewDataSource> {

   UITextField* textField;  //文本输入框

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

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

    UITableView*listView;    //下拉列表

   CGRect oldFrame, newFrame;  //整个控件(包括下拉前和下拉后)的矩形

   UIColor *lineColor,*listBgColor;//下拉框的边框色、背景色

   CGFloat lineWidth;             //下拉框边框粗细

   UITextBorderStyle borderStyle;  //文本框边框style

}

@property(nonatomic,retain)UITextField*textField;

@property(nonatomic,retain)NSArray* list;

@property(nonatomic,retain)UITableView*listView;

@property(nonatomic,retain)UIColor*lineColor,*listBgColor;

@property(nonatomic,assign)UITextBorderStyleborderStyle;

-(void)drawView;

-(void)setShowList:(BOOL)b;

@end


.m文件中实现:

 

#import "DropDownList.h"

@implementation DropDownList

@synthesizetextField,list,listView,lineColor,listBgColor,borderStyle;

-(id)initWithFrame:(CGRect)frame {

 

   if(self=[super initWithFrame:frame]){

//默认的下拉列表中的数据

list=[[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];

 

borderStyle=UITextBorderStyleRoundedRect;

 

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

oldFrame=frame;//未下拉时控件初始大小

//当下拉框显示时,计算出控件的大小。

newFrame=CGRectMake(frame.origin.x, frame.origin.y,frame.size.width, frame.size.height*5);

 

lineColor=[UIColorlightGrayColor];//默认列表边框线为灰色

listBgColor=[UIColorwhiteColor];//默认列表框背景色为白色

lineWidth=1;    //默认列表边框粗细为1

 

//把背景色设置为透明色,否则会有一个黑色的边

self.backgroundColor=[UIColor clearColor];

[selfdrawView];//调用方法,绘制控件

 

   }

   return self;

}

-(void)drawView{

   //文本框

   textField=[[UITextField alloc]

 initWithFrame:CGRectMake(0, 0,

oldFrame.size.width, 

oldFrame.size.height)];

textField.backgroundColor = [UIColorclearColor];

textField.opaque =YES;

textField.textColor= [UIColor whiteColor];

textField.font =[UIFont fontWithName:@"Arial" size:20.0];

textField.textAlignment = UITextAlignmentLeft;

   [self addSubview:textField];

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

 

   //下拉列表

   listView=[[UITableViewalloc]initWithFrame:

 CGRectMake(lineWidth,oldFrame.size.height+lineWidth, 

oldFrame.size.width-lineWidth*2,

oldFrame.size.height*4-lineWidth*2)];

   listView.dataSource=self;

   listView.delegate=self;

   listView.backgroundColor=listBgColor;

   listView.separatorColor=lineColor;

   listView.hidden=!showList;//一开始listView是隐藏的,此后根据showList的值显示或隐藏

 

   [selfaddSubview:listView]; 

   [listView release];

}

-(void)dropdown{

   [textField resignFirstResponder];

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

return;

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

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

 

[self.superviewbringSubviewToFront:self];

[selfsetShowList:YES];//显示下拉框

   }

}

#pragma marklistViewdataSource method and delegate method

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

   return list.count;

}

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

   static NSString*cellid=@"listviewid";

   UITableViewCell* cell=[tableViewdequeueReusableCellWithIdentifier:cellid];

   if(cell==nil){

cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefault

reuseIdentifier:cellid]autorelease];

   }

   //文本标签

   cell.textLabel.text=(NSString*)[listobjectAtIndex:indexPath.row];

   cell.textLabel.font=textField.font;

 

  cell.selectionStyle=UITableViewCellSelectionStyleGray;

   return cell;

}

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

   return oldFrame.size.height;

}

//当选择下拉列表中的一行时,设置文本框中的值,隐藏下拉列表

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

   //NSLog(@"select");

   textField.text = (NSString *)[listobjectAtIndex:indexPath.row];

//NSLog(@"1");

  //NSLog(@"textField.text=%@",textField.text);

   [self setShowList:NO];

}

-(BOOL)showList{//setShowList:No为隐藏,setShowList:Yes为显示

   return showList;

}

-(void)setShowList:(BOOL)b{

   showList=b;

   NSLog(@"showlist is set ");

   if(showList){

self.frame=newFrame;

   }else {

self.frame=oldFrame;

   }

   listView.hidden=!b;

}

 

- (void)dealloc{

[superdealloc];

}

@end

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值