#import<UIKit/UIKit.h>
@interfaceDropDownList:UIView<UITableViewDelegate,UITableViewDataSource> {
}
@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 {
//默认的下拉列表中的数据
list=[[NSArrayalloc]initWithObjects:@"1",@"2",@"3",@"4",nil];
borderStyle=UITextBorderStyleRounded
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;
//把背景色设置为透明色,否则会有一个黑色的边
self.backgroundColor=[UIColor clearColor];
[selfdrawView];//调用方法,绘制控件
}
-(void)drawView{
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;
[textFieldaddTarget:self action:@selector(dropdown)forControlEvents:UIControlEventAllTouchEv
oldFrame.size.width-lineWidth*2,
oldFrame.size.height*4-lineWidth*2)];
}
-(void)dropdown{
return;
//把dropdownList放到前面,防止下拉框被别的控件遮住
[self.superviewbringSubviewToFront:self];
[selfsetShowList:YES];//显示下拉框
}
#pragma marklistViewdataSource method and delegate method
-(NSInteger)tableView:(UITableView *)tablenumberOfRowsInSection:(NSInteger)section{
}
-(UITableViewCell*)tableView:(UITableView *)tableViewcellForRowAtIndexPath:(NSIndexPath *)indexPath{
cell=[[[UITableViewCellalloc]initWithStyle:UITableViewCellStyleDefa
reuseIdentifier:cellid]autorelease];
}
-(CGFloat)tableView:(UITableView *)tableViewheightForRowAtIndexPath:(NSIndexPath *)indexPath{
}
//当选择下拉列表中的一行时,设置文本框中的值,隐藏下拉列表
-(void)tableView:(UITableView *)tableViewdidSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//NSLog(@"1");
}
-(BOOL)showList{//setShowList:No为隐藏,setShowList:Yes为显示
}
-(void)setShowList:(BOOL)b{
self.frame=newFrame;
self.frame=oldFrame;
}
- (void)dealloc{
[superdealloc];
}
@end