代码如下:
#import <UIKit/UIKit.h>
@interface DropDownList : 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)UITextBorderStyle borderStyle;
-(void)drawView;
-(void)setShowList:(BOOL)b;
@end
#import "DropDownList.h"
@implementationDropDownList
@synthesize textField,list,listView,lineColor,listBgColor,borderStyle;
- (id)initWithFrame:(CGRect)frame {
if(self=[super initWithFrame:frame]){
//默认的下拉列表中的数据
list=[[NSArray alloc]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=[UIColor lightGrayColor];//默认列表边框线为灰色
listBgColor=[UIColor whiteColor];//默认列表框背景色为白色
lineWidth=1; //默认列表边框粗细为1
//把背景色设置为透明色,否则会有一个黑色的边
self.backgroundColor=[UIColor clearColor];
[self drawView];//调用方法,绘制控件
}
returnself;
}
-(void)drawView{
//文本框
textField=[[UITextField alloc]
initWithFrame:CGRectMake(0, 0,
oldFrame.size.width,
oldFrame.size.height)];
textField.borderStyle=borderStyle;//设置文本框的边框风格
[self addSubview:textField];
[textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
#import <UIKit/UIKit.h>
@interface DropDownList : 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)UITextBorderStyle borderStyle;
-(void)drawView;
-(void)setShowList:(BOOL)b;
@end
#import "DropDownList.h"
@implementationDropDownList
@synthesize textField,list,listView,lineColor,listBgColor,borderStyle;
- (id)initWithFrame:(CGRect)frame {
if(self=[super initWithFrame:frame]){
//默认的下拉列表中的数据
list=[[NSArray alloc]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=[UIColor lightGrayColor];//默认列表边框线为灰色
listBgColor=[UIColor whiteColor];//默认列表框背景色为白色
lineWidth=1; //默认列表边框粗细为1
//把背景色设置为透明色,否则会有一个黑色的边
self.backgroundColor=[UIColor clearColor];
[self drawView];//调用方法,绘制控件
}
returnself;
}
-(void)drawView{
//文本框
textField=[[UITextField alloc]
initWithFrame:CGRectMake(0, 0,
oldFrame.size.width,
oldFrame.size.height)];
textField.borderStyle=borderStyle;//设置文本框的边框风格
[self addSubview:textField];
[textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
本文介绍了一个自定义的 iOS 下拉列表视图组件,该组件基于 UIView 实现,并集成了 UITextField 和 UITableView 的功能。文章详细展示了如何通过 Objective-C 代码创建并配置下拉列表视图,包括设置默认数据、样式和行为。
2212

被折叠的 条评论
为什么被折叠?



