http://blog.163.com/ytrtfhj@126/blog/static/8905310920116224445195/
通过网上资料,自己借助资料写的代码,这是完全代码
//****************************************************************************
@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];
[tableArrayrelease];
[textFieldrelease];
[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 = [UIColorgrayColor];
tv.separatorColor = [UIColorlightGrayColor];
tv.hidden = YES;
[self addSubview:tv];
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
textField.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格
[textFieldaddTarget:selfaction:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
[self addSubview:textField];
}
returnself;
}
-(void)dropdown{
[textFieldresignFirstResponder];
if (showList) {//如果下拉框已显示,什么都不做
return;
}else {//如果下拉框尚未显示,则进行显示
CGRect sf = self.frame;
sf.size.height = frameHeight;
//把dropdownList放到前面,防止下拉框被别的控件遮住
[self.superviewbringSubviewToFront:self];
tv.hidden = NO;
showList = YES;//显示下拉框
CGRect frame = tv.frame;
frame.size.height = 0;
tv.frame = frame;
frame.size.height = tabheight;
[UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];
[UIViewsetAnimationCurve:UIViewAnimationCurveLinear];
self.frame = sf;
tv.frame = frame;
[UIViewcommitAnimations];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableArraycount];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCellalloc] initWithStyle:UITableViewCellStyleDefaultreuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];
cell.textLabel.font = [UIFontsystemFontOfSize: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 = [[DropDown1alloc] initWithFrame:CGRectMake(10, 10, 140, 100)];
dd1.textField.placeholder = @"请输入联系方式";
NSArray* arr=[[NSArrayalloc]initWithObjects:@"电话",@"email",@"手机",@"aaa",@"bbb",@"ccc",nil];
dd1.tableArray = arr;
[arr release];
[self.view addSubview:dd1];
[dd1 release];
相关推荐
在iOS开发中,下拉菜单是一种常见的用户界面元素,它为用户提供了一种简洁的方式来选择或浏览一...开发者可以通过研究`DropMenu-demo`示例来学习如何在自己的iOS应用中集成和定制这个下拉菜单,提升应用的交互体验。
本资源"ios-导航条自定义下拉菜单.zip"提供了一个名为ThirdMenuDemo的示例项目,帮助开发者学习如何在导航条上创建和实现自定义的下拉菜单。 首先,自定义导航条下拉菜单的核心在于利用...
- **适配不同屏幕尺寸**:确保你的下拉菜单能适应不同设备的屏幕大小,特别是iPhone和iPad之间的差异。 - **响应式设计**:根据用户交互情况动态调整菜单的显示,如在键盘弹出时自动隐藏菜单以避免遮挡输入。 6. ...
在iOS开发中,下拉菜单(Dropdown Menu)是一种常见的用户界面元素,用于提供多项选择,通常出现在顶部或者底部,点击后会展示一个可选列表。本项目“ios-下拉菜单的封装.zip”中包含了一个名为JKDropDownMenu的组件...
在iOS开发中,"ios-cell点击下拉.zip" 这个资源可能是一个示例项目或者教程,用于演示如何实现UITableView的单元格(Cell)在点击后展开下拉内容。这种功能常见于各种需要显示详细信息或者多级选择的场景,如菜单、...
- 在Xcode中选择项目,然后在目标设备下拉菜单中选择你的iOS设备。 - 编译并运行你的应用,Xcode会自动部署到设备上并开始调试。 2. **iOS 11.4的关键特性**: - Siri Shortcuts:这是一个自动化任务的功能,...
Swift-DropDown 是一款针对 iOS 平台的 UI 控件,它实现了 Material Design 风格的下拉菜单。Material Design 是谷歌推出的一种设计语言,以其简洁、直观和响应式的界面设计著称,广泛应用于 Android 和 iOS 开发。...
本资源“(0031)-iOS/iPhone/iPAD/iPod源代码-列表(Table)-comboBox”着重于如何利用UITableView实现一个下拉菜单的效果,这在很多应用中是非常实用的功能,例如用户选择日期、时间或者特定选项时。...
在iOS中,菜单通常指的是UI控件,如下拉列表、滑动菜单(滑动视图)、汉堡菜单(抽屉式导航)等。这些菜单在屏幕的不同位置出现,为用户提供多种选项,帮助他们导航应用程序或执行特定任务。例如,汉堡菜单常用于在...
在本教程中,我们将深入探讨如何使用Objective-C(OC)来实现这样的下拉菜单。 一、Swift与Objective-C的区别 虽然标题中提到的是Objective-C,但值得一提的是,现在的iOS开发趋势更倾向于使用Swift。然而,...
4. **扩展功能**:例如增加下拉菜单、滑动切换等功能,提供更多操作入口。 在实现自定义TabBar的过程中,开发者通常会用到以下技术: - **Swift或Objective-C**:iOS开发的两种主要编程语言,用于编写TabBar的逻辑...
这些控件是Apple提供的原生UI组件,能够展示一系列可滚动的数据项,非常适合构建类似下拉菜单的效果。本教程将深入探讨如何在iPhone应用中实现下拉列表功能。 首先,我们需要了解UITableView的基本概念。...
本教程将详细讲解如何利用UITableView实现一个可扩展的下拉菜单效果,适用于iPhone、iPad以及iPod设备。这种效果允许用户点击列表中的某一行,然后展开更多的选项供用户选择。 首先,我们要理解UITableView的基本...
总之,下拉菜单在iOS应用中扮演着重要角色,开发者需要根据具体需求选择合适的实现方式,并确保良好的交互性和用户体验。无论是标准的Picker View还是自定义的下拉菜单,都需要细心设计和实现,以提高应用的可用性和...
jQuery Promptu-menu是一款专为iPhone设计的菜单滑动插件,它能够帮助开发者创建出具有流畅动画效果、符合iOS用户习惯的下拉菜单,提升网站在手机端的导航体验。本文将深入探讨这个插件的工作原理、特点以及如何在...
这些元素可能包括但不限于按钮、输入框、导航栏、标签页、滑块、开关、警告对话框等常见的交互控件,以及特定于iOS系统的组件如Pull-to-Refresh、下拉菜单、日期选择器等。 设计者可以利用这些部件库中的元素快速...
2. **使用UIPopoverPresentationController**(iOS 8及以上):这是替代UIPopoverController的新API,可以创建类似的效果,但不仅限于iPad,也可以在iPhone上使用。 3. **自定义View**:可以创建一个自定义的UIView...
4. **UI组件丰富**:包含各种常见的UI组件,如分页控件、标签栏、下拉菜单等。 三、`ios-calendar`的使用步骤 1. **集成Three20库**:首先需要将Three20库引入项目中,可以通过CocoaPods或手动添加库文件。 2. **...
考虑到不同设备的屏幕尺寸,确保左右滑动菜单在iPhone和iPad等不同设备上都能正常工作,需要使用Auto Layout或SwiftUI的GeometryReader来实现响应式布局。 10. **手势冲突解决**: 在某些情况下,滑动菜单可能与...
本教程将深入探讨如何使用Storyboard来实现一个适用于iPad和iPhone的多级菜单。 首先,我们需要理解Storyboard在iOS开发中的作用。Storyboard是Xcode提供的一种可视化设计工具,开发者可以在这个界面上直接布局UI...