【IOS功能实现】之:下拉列表
通过网上资料,自己借助资料写的代码,这是完全代码
//****************************************************************************
@interface DropDown1 : UIView <UITableViewDelegate,UITableViewDataSource> {
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
//****************************************************************************
@implementation DropDown1
@synthesize tv,tableArray,textField;
- (void)dealloc
{
[tv release];
[tableArray release];
[textField release];
[super dealloc];
}
-(id)initWithFrame:(CGRect)frame
{
if (frame.size.height<200) {
frameHeight = 200;
}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, 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];
}
return self;
}
-(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] autorelease];
}
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
//****************************************************************************
上面的是实现方法,下面是使用:
DropDown1 *dd1 = [[DropDown1 alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
dd1.textField.placeholder = @"请输入联系方式";
NSArray* arr=[[NSArray alloc]initWithObjects:@"电话",@"email",@"手机",@"aaa",@"bbb",@"ccc",nil];
dd1.tableArray = arr;
[arr release];
[self.view addSubview:dd1];
[dd1 release];
【IOS功能实现】之:下拉列表相关推荐
- iOS功能-统计平均下班时间
iOS功能-统计平均下班时间 因为大厂上班有个隐形的参数:平均下班时间.需求:统计每天的下班打开时间,计算出当前月平均下班时间. 规则:只统计18:00-24:00之间的下班打开时间,排除下午请假以及 ...
- 基于SYD8801的蓝牙遥控器设计方案【按键功能】【安卓手机功能演示】【ios功能演示】
SYD8801是一款低功耗高性能蓝牙低功耗SOC,集成了高性能2.4GHz射频收发机.32位ARM Cortex-M0处理器.128kB Flash存储器.以及丰富的数字接口.SYD8801片上集成了 ...
- IOS 一个很好的录制屏幕实现IOS功能
转自 https://github.com/wf1992/WFRecScreenDemo 和 http://www.jianshu.com/p/9a23b5365f0f 很不错 去看看 前文提要: 最 ...
- 【分享】iOS功能界面漂亮的弹出框
STPopup 为 iPhone 和 iPad提供了 STPopupController UINavigationController 弹出的风格. 特性: Extend your view cont ...
- iOS中Safari浏览器select下拉列表文字太长被截断的处理方法
网页中的select下拉列表,文字太长的话在iOS的Safari浏览器里会被自动截断,显示成下面这种: 安卓版的浏览器则没有这个问题. 如何让下拉列表中的文字在iOS的Safari浏览器里显示完整呢? ...
- 常用的几个提高iOS开发效率的开源类库及工具
转自 iOS开发者 : 几个常用的开源类库及下载地址: 算上这个连接:http://wenku.baidu.com/view/bbeb0091dd88d0d233d46a00.html 1.json ...
- 化解Android 和iOS“恩怨”的Flutter ,凭的是什么?
移动研发由来已久,从早期的 J2ME 开始,到后来居上的 Windows Mobile . Symbian ,小众却各领风骚的 BREW . Palm . BlackBerry ,以及当前几乎平分市场 ...
- IOS开发之----资源汇总
学习过程当中查找到的资料,做一个记录,以便更多朋友查看. 开发的过程有时候是简单的,因为我们可以站在巨人的肩上,跳得更高,走得更远. 如何用Facebook graphic api上传视频: http ...
- iOS 的一些资源汇总
UI界面类项目: Panoramagl -- 720全景展示 Panorama viewer library for iPhone, iPad and iPod touch MBProgressHUD ...
最新文章
- 健身提示:跑步一周只需三次 天天跑易疲劳
- centos误删除文件如何恢复
- 根据当前系统日期求出本周,本月,本年,上周,上月,上年的第一天
- [IOI2008]Island
- PYTHON系列-从零开始的爬虫入门指南
- java spring注解_spring注解是如何实现的?
- java给文件添加水印_Java在PDF中添加水印(文本/图片水印)
- open表和closed表_什么是左外连接?左外连接在工作表查询中的应用
- python时间操作代码
- 如何让应用出现在LINUX的右键/打开方式/更多应用
- 详细的log4j配置使用流程
- 测试睡眠质量软件,测睡眠质量的app排行榜:推荐10款有趣又有效的睡眠APP
- 金山、百度、360上演“三国杀” 巨头争推网盘
- ubuntu 关机后 自动重启 解决方法
- 不同等级的UI设计师工作内容有什么区别?
- 《网络攻防》 免杀原理与实践
- 我好像上瘾了-王者农药(脱坑篇)
- luogu P4438 [HNOI/AHOI2018]道路
- c语言描述abc表达式cba,2015年3月全国计算机二级C语言选择第2套
- 操作系统期末试卷解析(1)
热门文章
- c语言:输入一个整数n,计算n!
- 关于python的列表以下选项中描述错误的是_以下关于Python列表的描述中,错误的是______...
- guvcview调试UVC摄像头
- cesium实现添加在线地图的偏移纠正
- 探索性数据分析(EDA)为什么在机器学习中至关重要?
- 快速傅里叶变换(FFT),离散傅里叶变换(DFT)
- 卡特兰数 Catalan
- python 源代码 macd双底 高 低_中国股市真正厉害的人:MACD双顶、双底,晚上睡不着时就看看...
- [转]刨根问底!曲面细分技术到底是个啥?
- JZOJ2018.07.12【2018提高组】模拟B组 挑竹签