#import

@interface DropDown1 : UIView <</span>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

#import "DropDown1.h"

@implementation DropDown1

@synthesize tv,tableArray,textField;

- (void)dealloc

{

[tv release];

[tableArray release];

[textField release];

[super dealloc];

}

-(id)initWithFrame:(CGRect)frame

{

if (frame.size.height<<span style="color: #2934d5">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学习--iphone 实现下拉菜单的更多相关文章

  1. iphone动态下拉菜单

    介绍:实现带动画效果的下拉菜单.用户按下菜单按钮,出现下拉按钮,用户松开菜单按钮,下拉按钮收回. 测试环境:Xcode 4.3, iOS 5.0. 效果图: jQuery特效:http://www.h ...

  2. BootStrap学习(2)_下拉菜单&按钮组

    一.下拉菜单 1.基本下拉菜单 如需使用下列菜单,只需要在class .dropdown 内加上下拉菜单即可.下面的实例演示了基本的下拉菜单: <!DOCTYPE html> <ht ...

  3. Bootstrap3.0学习第十轮(下拉菜单、按钮组、按钮式下拉菜单)

    详情请查看http://aehyok.com/Blog/Detail/16.html 个人网站地址:aehyok.com QQ 技术群号:206058845,验证码为:aehyok 本文文章链接:ht ...

  4. Bootstrap 学习笔记8 下拉菜单滚动监听

    代码部分: <nav class="navbar navbar-default"> <a href="#" class="navba ...

  5. 如何在webapp中做出原生的ios下拉菜单效果

    github:https://github.com/zhoushengmufc/iosselect webapp模仿ios下拉菜单 html下拉菜单select在安卓和IOS下表现不一样,iossel ...

  6. IOS 下拉菜单

    由于之前曾经用到过下拉菜单,所以现在花一些时间回过头来细细整理了一下,逐步完善这个下拉菜单,并提供一些比较基本的功能,以便日后如果有需要的话可以进行复用,并提供给需要的人参考.下拉菜单同样分为数据源和 ...

  7. Android:有关下拉菜单导航的学习(供自己参考)

    Android:有关==下拉菜单导航==的学习 因为先前的学习都没想着记录自己的学习历程,所以该博客才那么迟才开始写. 内容: ==下拉菜单导航== 学习网站:android Spinner控件详解 ...

  8. IOS第二天-新浪微博 - 添加搜索框,弹出下拉菜单 ,代理的使用 ,HWTabBar.h(自定义TabBar)

    ********HWDiscoverViewController.m(发现) - (void)viewDidLoad { [super viewDidLoad]; // 创建搜索框对象 HWSearc ...

  9. iOS 下拉菜单 FFDropDownMenu自定义下拉菜单样式实战-b

    Demo地址:https://github.com/chenfanfang/CollectionsOfExampleFFDropDownMenu框架地址:https://github.com/chen ...

随机推荐

  1. Win10远程桌面提示你的凭据不工作的处理方法

    需要确保在组策略编辑器(Win+R 输入 gpedit.msc )中计算机配置->Windows设置->安全设置->本地策略->安全选项->右侧的网络访问:本地帐户的共享 ...

  2. SCF: 简单配置门面[转]

    原文:https://blog.csdn.net/koqizhao/article/details/82178100 Simple Configuration Facade :简单配置门面  是 代码 ...

  3. RobotFrameWork接口设计规范

    1. 前言 继前面一章<RobotFramework环境搭建>介绍了在本地如何将接口自动化实施过程所需要的基础环境搭建好,在这里假设大家都已经知道环境如何搭建了,如果不清楚的可直接查看上一 ...

  4. Pinterest凭什么拥有那么多用户:机器学习是答案

    目前,Pinterest月平均活跃用户量达到1亿,这家以图片为主的公司是如何留住用户并盈利的呢?Pinterest的主要目标是向用户推荐相关的图片或内容,推荐的内容足够精确才能提高用户黏性.近期,&l ...

  5. html5利用websocket完成的推送功能(tomcat)

    html5利用websocket完成的推送功能(tomcat) 利用websocket和java完成的消息推送功能,服务器用的是tomcat7.0.42,一些东西是自己琢磨的,也不知道恰不恰当,不恰当 ...

  6. Python 获得最近一个月的每天的日期

    直接上代码: #coding:utf-8 # from common.contest import * import datetime import time begin_date = (dateti ...

  7. PHP 开发者的 Docker 之旅

    用 PHP 作为我们「Docker 开发大礼包」开篇是带着一些朝圣的心情的.这是一门堪称「古老」的语言,这也是一门争议最多的语言,这更是一门不断涅槃的语言.「PHP 是最好的语言」这个流传已久的梗,或 ...

  8. 【转载】MapReduce编程 Intellij Idea配置MapReduce编程环境

    目录(?)[-] 一软件环境 二创建maven工程 三添加maven依赖 四配置log4j 五启动Hadoop 六运行WordCount从本地读取文件 七运行WordCount从HDFS读取文件 八代 ...

  9. Qt学习记录--02 Qt的信号槽机制介绍(含Qt5与Qt4的差异对比)

    一 闲谈: 熟悉Window下编程的小伙伴们,对其消息机制并不陌生, 话说:一切皆消息.它可以很方便实现不同窗体之间的通信,然而MFC库将很多底层的消息都屏蔽了,尽管使用户更加方便.简易地处理消息,但 ...

  10. 【转载】Docker 安装后 报 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running? 解决办法

    Docker Docker 安装后 报 Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docke ...