一個列表選擇型textfield控件息楔,只需設置控件位置并添加到所在視圖即可,用法相當簡單示例代碼如下:在要用到類的.m文件里倒入DropDown.h文件扒披,并創(chuàng)建控件全局屬性值依,在合適方法中把控件添加到控件需要在的圖層即可。
用法:
- (void)viewDidLoad{? ?
[super viewDidLoad];? ?
?? _dropDown = [[DropDown alloc]initWithFrame:CGRectMake(80, self.view.bounds.size.height*0.5-70,KScreenWidth-160, KScreenHeight- 200)];?
? NSArray *ar = @[@"1000",@"2000",@"3000",@"4000",@"5000",@"10000",@"20000",@"30000"];? ?
_dropDown.tableArray = ar;? ?
[self.view addSubview:_dropDown];??
}
- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event {
[_dropDown resignFirstResponder];
}
DropDown控件的.h和.m文件 ------“直接粘貼即可”
DropDown.h文件
#import<UIKit/UIKit.h>
@interface DropDown:UIView<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
{
UITableView *tv;//下拉列表
NSArray *tableArray;//下拉列表數(shù)據(jù)
UITextField *textField;//文本輸入框
BOOL showList;//是否彈出下拉列表
CGFloat tabheight;//table下拉列表的高度
CGFloat frameHeight;//frame的高度
}
@property (nonatomic,retain) UITableView *tv;
@property (nonatomic,retain) NSArray *tableArray;
@property (strong, nonatomic) NSDictionary *pickerDic;//獲取文件里的字典
@property (strong, nonatomic) NSArray *provinceArray;//省碟案、市
@property (nonatomic,retain) UITextField *textField;
@property (assign, nonatomic) BOOL showList;
-(void)dontshowlist;
@end
DropDown.m文件
#import "DropDown.h"
@implementation DropDown
@synthesize tv,tableArray,textField,showList;
-(id)initWithFrame:(CGRect)frame
{
if (frame.size.height<300) {
frameHeight =300;
}else{
frameHeight = frame.size.height;
}
tabheight = frameHeight-30;
frame.size.height = 30.0f;
self=[super initWithFrame:frame];
if(self){
showList = NO; //默認不顯示下拉框
tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width,300)];
tv.delegate = self;
tv.dataSource = self;
tv.separatorColor = [UIColor lightGrayColor];
tv.hidden = YES;
tv.contentOffset =CGPointMake(0, 64);
[self addSubview:tv];
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
textField.borderStyle=UITextBorderStyleRoundedRect;//設置文本框的邊框風格
[textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
textField.font = [UIFont systemFontOfSize:16];
textField.placeholder = @"請選擇(長按)";
//設置textfield 不彈出鍵盤
textField.inputView=[[UIView alloc]initWithFrame:CGRectZero];
textField.backgroundColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1];
[self addSubview:textField];
NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];
self.pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];
//省? 市
self.provinceArray = [self.pickerDic allKeys];
tableArray = self.provinceArray;
}
return self;
}
//不展示列表
-(void)dontshowlist
{
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;
}
-(void)dropdown
{
//? ? [tv reloadData];
[textField resignFirstResponder];
if (showList) {//如果下拉框已顯示愿险,什么都不做
[textField resignFirstResponder];
return;
}else {//如果下拉框尚未顯示,則進行顯示
[textField resignFirstResponder];
CGRect sf = self.frame;
sf.size.height = frameHeight;
//把dropdownList放到前面价说,防止下拉框被別的控件遮住
[self.superview bringSubviewToFront:self];
tv.hidden = NO;
showList = YES;//顯示下拉框
tv.tableHeaderView.backgroundColor = [UIColor blackColor];
CGRect frame = tv.frame;
frame.size.height = 0;
tv.frame = frame;
frame.size.height = tabheight;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
self.frame = sf;
tv.frame = frame;
[UIView commitAnimations];
}
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [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.backgroundColor = [UIColor grayColor];
cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];
cell.textAlignment = UITextAlignmentCenter;
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
{
textField.text = [tableArray objectAtIndex:[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;
NSString *string = [NSString stringWithFormat:@"%ld",(long)[indexPath row]];
NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:string,@"textOne",nil];
//創(chuàng)建通知
NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];
//通過通知中心發(fā)送通知
[[NSNotificationCenter defaultCenter] postNotification:notification];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end