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=[super initWithFrame:frame];
if(self){
showlist=NO;
self.tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 0)];
self.tv.delegate = self;
self.tv.dataSource = self;
self.tv.backgroundColor = [UIColor grayColor];
self.tv.separatorColor = [UIColor lightGrayColor];
self.tv.hidden = YES;
[self addSubview:self.tv];
self.textFeild = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
self.textFeild.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格
[self.textFeild addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
[self addSubview:self.textFeild];
}
return self;
}
-(void)dropdown{
[self.textFeild resignFirstResponder];
if (showlist) {//如果下拉框已显示,什么都不做
return;
}else {//如果下拉框尚未显示,则进行显示
CGRect sf = self.frame;
sf.size.height = frameHeight;
//把dropdownList放到前面,防止下拉框被别的控件遮住
[self.superview bringSubviewToFront:self];
self.tv.hidden = NO;
showlist = YES;//显示下拉框
CGRect frame =self.tv.frame;
frame.size.height = 0;
self.tv.frame = frame;
frame.size.height = tabHeight;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
self.frame = sf;
self.tv.frame = frame;
[UIView commitAnimations];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.textLabel.text = [self.tableArray objectAtIndex:[indexPath row]];
cell.textLabel.font = [UIFont systemFontOfSize: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.tableArray objectAtIndex:[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 = [[DropDown alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
dd1.textFeild.placeholder = @"请输入内容";
NSArray* arr=[[NSArray alloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",nil];
dd1.tableArray = arr;
[self.view addSubview:dd1];