#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. iOS https请求 NSURLSessionDataTask

    // //  YKSHttpsRequest.m //  YKShareSdkDemo // //  Created by qingyun on 22/05/2017. //  Copyright © ...

  2. JAVA项目中引用Logback的方法

    一.简介 本文主要讲JAVA项目中引入Logback的方法. 二.解决 1.引入依赖. <!--Begin LogBack Log--> <!-- https://mvnreposi ...

  3. 基于cefsharp的用户浏览器

    技术:vc++2015   概述 用于需要制作一个浏览器 winfrom 中浏览器的插件有很多种 如:WebBrowser , Web.kit等 但用于比较稳定 功能齐全的还是cefsharp 详细 ...

  4. LNMP环境的搭建(yum)方法(精)

    第一 先安装nginx nginx在官方CentOS社区yum里面没有,需要在nginx的官方网站去下载yum的配置文件 官方:https://www.nginx.com/resources/wiki ...

  5. ComputeShader中Counter类型的使用

    接上一篇:https://www.cnblogs.com/hont/p/10122129.html 除了Append类型对应的Consume/AppendStructuredBuffer还有一个Cou ...

  6. halcon 创建region的最大尺寸问题

    gen_region 之类的创建region 之前需要提前设置region的最大尺寸,设置方法如下: set_system('width',2000)set_system('height',2000) ...

  7. Elasticsearch index fields 重命名

    reindex数据复制,重索引 POST _reindex { "source": { "index": "twitter" }, &quo ...

  8. Web重温系列(二):SQLite+EF6实现本地化存储

    本来我们的产品有着复杂的层次结构,作为客户端的C# WinForm是不操作数据库的.但是最近有个需求,需要将数据本地保存.可选的方案很多,比如文本文件或者XML序列化和反序列化,或者如access.d ...

  9. MyBatis总结五:#{}和${}的用法和区别

    From: https://www.cnblogs.com/blazeZzz/p/9295634.html #{}的用法: 我们发现,在Mapper.xml映射文件中,经常使用#{属性名} 来作为SQ ...

  10. 基于物理规则的渲染(PBR)

    为毛我的效果那么挫,我也是按照公式来的 2017 -3 -20