#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. 【jQuery:遍历同样class的全部值,遍历某一列td的值】

    jsp代码: Html代码 <c:forEach var="main" items="${mainPage.list }"> <tr> ...

  2. Python随机字符串验证码

    def code_get(self): source = ['] #数字验证码 # source = [ 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H','I','J', ...

  3. c# mvc 封装返回对象

    将所有返回JsonContent对象进行再次封装 public class ResultFilterAttribute : System.Web.Mvc.ActionFilterAttribute { ...

  4. 14.翻译系列:从已经存在的数据库中生成上下文类和实体类【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/code-first-from-existing-database.aspx EF 6 ...

  5. Kubernetes 1.12公布:Kubelet TLS Bootstrap与Azure虚拟机规模集(VMSS)迎来通用版本号

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/M2l0ZgSsVc7r69eFdTj/article/details/82880341 https: ...

  6. AWS免费套餐服务器部署NETCORE网站

    之前的linode充了5美元,一个月就用完了,还是创建的最便宜的服务器的!!! 以前一直想用用aws的所谓的免费套餐服务器的,现在linode过期了,可以试着用用了 下面是我的操作流程,包含错误及解决 ...

  7. SQL递归方式实现省市区县级别查询

    数据库脚本 CREATE TABLE [dbo].[Std_Area]( [Id] [int] NOT NULL, [Name] [nvarchar](50) NULL, [ParentId] [in ...

  8. Canvas入门到高级详解(下)

    四. Canvas 开发库封装 4.1 封装常用的绘制函数 4.1.1 封装一个矩形 //思考:我们用到的矩形需要哪些绘制的东西呢? 矩形的 x.y坐标 矩形的宽高 矩形的边框的线条样式.线条宽度 矩 ...

  9. power designer 16.5 使用总结[转]

    测试环境:power designer 16.5.vs2010.win7 对于破解版的power designer经常出现崩溃.停止工作的情况 请运行pdlegacyshell16.exe,不要运行P ...

  10. test001

    #include <iostream> using namespace std; ][]; int main() { int num; ; ; i <= ; i++) dp[i][] ...