IOS 自定义下拉选择控件
2016-11-07 00:00
183 查看
.h文件代码
.m文件中代码
调用代码:(dataSource自定义数组)
关注新平台:http://www.toutiao.com/m51416718261/
关注订阅号:从小就坏
#import <UIKit/UIKit.h> @interface DropDown : UIView <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> { UITableView *tv;//下拉列表 NSArray *tableArray;//下拉列表数据 UITextField *textField;//文本输入框 BOOL showList;//是否弹出下拉列表 CGFloat tabheight;//table下拉列表的高度 CGFloat frameHeight;//frame的高度 } @property (nonatomic,retain) UITableView *tv; @property (nonatomic,retain) NSArray *tableArray; @property (nonatomic,retain) UITextField *textField; @end
.m文件中代码
#import "DropDown.h" @implementation DropDown @synthesize tv,tableArray,textField; -(id)initWithFrame:(CGRect)frame { if (frame.size.height<200) { frameHeight = 200; }else{ frameHeight = frame.size.height; } // tabheight = frameHeight-30; tabheight = 70; frame.size.height = 30.0f; self=[super initWithFrame:frame]; if(self){ showList = NO; //默认不显示下拉框 tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 0)]; tv.delegate = self; tv.dataSource = self; tv.backgroundColor = [UIColor grayColor]; tv.separatorColor = [UIColor lightGrayColor]; tv.hidden = YES; [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]; [self addSubview:textField]; textField.delegate = self;//这一句要加入代理 } return self; } - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{ return NO; } -(void)dropdown{ [textField resignFirstResponder]; if (showList) {//如果下拉框已显示,什么都不做 return; }else {//如果下拉框尚未显示,则进行显示 CGRect sf = self.frame; sf.size.height = frameHeight; //把dropdownList放到前面,防止下拉框被别的控件遮住 [self.superview bringSubviewToFront:self]; tv.hidden = NO; showList = YES;//显示下拉框 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)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (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.textLabel.text = [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 { 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; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } @end
调用代码:(dataSource自定义数组)
DropDown *dropDown = [[DropDown alloc] initWithFrame:CGRectMake(100, 40, 150, 30)]; dropDown.textField.placeholder = @"请选择购买时长"; dropDown.tableArray = dataSource; [self.view addSubview:dropDown];
关注新平台:http://www.toutiao.com/m51416718261/
关注订阅号:从小就坏
相关文章推荐
- iOS开发系列之常用自定义控件开发集—自定义下拉列表按钮控件开发
- iOS 自定义日期和数据源选择控件
- Android自定义组合控件之下拉选择框
- 源码推荐:自定义下拉刷新动画 弹性的分段选择控件
- 自定义组合控件:下拉选择框
- iOS自定义控件之下拉列表按钮
- 源码推荐:自定义下拉刷新动画 弹性的分段选择控件
- Android自定义View仿IOS选择控件Togglebutton实现
- 带有下拉选择控件的自定义对话框
- 自定义控件之------仿ios下拉回弹效果
- [iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
- ios下自定义时间选择控件
- 仿iOS的Pi 4000 ckerView控件,有时间选择和选项选择,并支持一二三级联动,支持自定义样式
- altas(ajax)控件(六):CascadingDropDown 联动选择的下拉框
- 自定义选择控件引出的笔记
- 转:选择好友的下拉控件(类型开心网的)
- 弹出下拉编辑控件,跟踪输入内容自动过滤查询,过滤条件完全自定义,支持TextBox,支持DataGridView(弹出查询对话框),支持DevExpress 8.2的TextEdit,Gridview
- Silverlight DateTimePicker 自定义的日期及时间选择控件
- jQuery插件 - 下拉列表日期选择控件
- 自定义一个选择日期的用户控件