#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. 【PMP】组织结构类型

    1.简单型 描述:人员并肩工作,所有者/经营者直接做出主要决定并监督执行. PM角色:兼职(协调员) PM权限:极少(无) 项目管理人员:极少(无) 资源可用性:极少(无) 项目预算管理人:负责人 2 ...

  2. c# 基于redis分布式锁

    在单进程的系统中,当存在多个线程可以同时改变某个变量(可变共享变量)时,就需要对变量或代码块做同步,使其在修改这种变量时能够线性执行消除并发修改变量. 而同步的本质是通过锁来实现的.为了实现多个线程在 ...

  3. Mybatis判断map参数是否存在

    <select id="selectByCondition" parameterType="java.util.HashMap" resultMap=&q ...

  4. 射频与微波测量之S参数

    转自:https://www.cnblogs.com/lyh523329053/p/9128577.html S参数 S散射也叫散射参数.是微波传输中的一组重要参数.由于我们很难在高频率时测量电流或电 ...

  5. NodeJs之fs

    NodeJs版本:4.4.4 fs的实用方法 查看文件信息(fs.stat) 定义:fs.stat(path, callback) var fs = require('fs'); fs.stat('t ...

  6. [svc]证书学习索引

    数字证书基础知识 对称加密/非对称加密细枝末节 openssl对称非对称加密实战 使用OpenSSL实现CA证书的搭建过程 通过openssl生成证书 HTTPS证书生成原理和部署细节 证书各个字段的 ...

  7. [moosefs] storage class

    chapter 1 moosefs 3.1 storage class 功能的介绍 1.1 什么是storage class 在moosefs中,storage class允许指定文件的chunks存 ...

  8. python用matplotlib画折线图

    折线图: import matplotlib.pyplot as plt y1=[10,13,5,40,30,60,70,12,55,25] x1=range(0,10) x2=range(0,10) ...

  9. 安防工程商必须知道的PoE供电真相

    问题一:何为PoE技术? PoE (Power Over Ethernet)指的是在现有的以太网Cat.5布线基础架构不作任何改动的情况下,在为一些基于IP的终端(如IP电话机.无线局域网接入点AP. ...

  10. 【GMT43智能液晶模块】例程十三:FATFS实验——文件操作

    实验原理: STM32F429上带有SDIO控制器,GMT43液晶模块上将SDIO连接到TF卡座.本实验 将Micro SD卡插入TF卡座上即可.通过FATFS创建test.txt文件,并且写入数据0 ...