`
jsntghf
  • 浏览: 2511441 次
  • 性别: Icon_minigender_1
  • 来自: 苏州
社区版块
存档分类
最新评论

下拉列表的实现

    博客分类:
  • iOS
阅读更多

ComboBoxView.h

 

#import <UIKit/UIKit.h>

@interface ComboBoxView : UIView <UITableViewDelegate, UITableViewDataSource> {
	UILabel			*selectContentLabel;
	UIButton		*pulldownButton;
	UIButton		*hiddenButton;
	UITableView		*comboBoxTableView;
	NSArray			*comboBoxDatasource;
	BOOL			showComboBox;
}

@property (nonatomic, retain) NSArray *comboBoxDatasource;

- (void)initVariables;
- (void)initCompentWithFrame:(CGRect)frame;
- (void)setContent:(NSString *)content;
- (void)show;
- (void)hidden;
- (void)drawListFrameWithFrame:(CGRect)frame withContext:(CGContextRef)context;

@end

 

ComboBoxView.m

 

#import "ComboBoxView.h"

@implementation ComboBoxView

@synthesize comboBoxDatasource;

- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
		[self initVariables];
		[self initCompentWithFrame:frame];
    }
    return self;
}

#pragma mark -
#pragma mark custom methods

- (void)initVariables {
	showComboBox = NO;
}

- (void)initCompentWithFrame:(CGRect)frame {
	selectContentLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 0, frame.size.width - 45, 25)];
	selectContentLabel.font = [UIFont systemFontOfSize:14.0f];
	selectContentLabel.backgroundColor = [UIColor clearColor];
	[self addSubview:selectContentLabel];
	[selectContentLabel release];
	
	pulldownButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[pulldownButton setFrame:CGRectMake(frame.size.width - 25, 0, 25, 25)];
	[pulldownButton setBackgroundImage:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"down_icon" ofType:@"png"]]
							   forState:UIControlStateNormal];
	[pulldownButton addTarget:self action:@selector(pulldownButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:pulldownButton];
	
	hiddenButton = [UIButton buttonWithType:UIButtonTypeCustom];
	[hiddenButton setFrame:CGRectMake(0, 0, frame.size.width - 25, 25)];
	hiddenButton.backgroundColor = [UIColor clearColor];
	[hiddenButton addTarget:self action:@selector(pulldownButtonWasClicked:) forControlEvents:UIControlEventTouchUpInside];
	[self addSubview:hiddenButton];
	
	comboBoxTableView = [[UITableView alloc] initWithFrame:CGRectMake(1, 26, frame.size.width -2, frame.size.height - 27)];
	comboBoxTableView.dataSource = self;
	comboBoxTableView.delegate = self;
	comboBoxTableView.backgroundColor = [UIColor clearColor];
	comboBoxTableView.separatorColor = [UIColor blackColor];
	comboBoxTableView.hidden = YES;
	[self addSubview:comboBoxTableView];
	[comboBoxTableView release];
}

- (void)setContent:(NSString *)content {
	selectContentLabel.text = content;
}

- (void)show {
	comboBoxTableView.hidden = NO;
	showComboBox = YES;
	[self setNeedsDisplay];
}

- (void)hidden {
	comboBoxTableView.hidden = YES;
	showComboBox = NO;
	[self setNeedsDisplay];
}

#pragma mark -
#pragma mark custom event methods

- (void)pulldownButtonWasClicked:(id)sender {
	if (showComboBox == YES) {
		[self hidden];
	}else {
		[self show];
	}
}

#pragma mark -
#pragma mark UITableViewDelegate and UITableViewDatasource methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [comboBoxDatasource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	static NSString *CellIdentifier = @"ListCellIdentifier";
	UITableViewCell *cell = [comboBoxTableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}
	cell.textLabel.text = (NSString *)[comboBoxDatasource objectAtIndex:indexPath.row];
	cell.textLabel.font = [UIFont systemFontOfSize:13.0f];
	cell.accessoryType = UITableViewCellAccessoryNone;
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	
	return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	return 25.0f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[self hidden];
	selectContentLabel.text = (NSString *)[comboBoxDatasource objectAtIndex:indexPath.row];
}

- (void)drawListFrameWithFrame:(CGRect)frame withContext:(CGContextRef)context {
	CGContextSetLineWidth(context, 2.0f);
	CGContextSetRGBStrokeColor(context, 0.0f, 0.0f, 0.0f, 1.0f);
	if (showComboBox == YES) {
		CGContextAddRect(context, CGRectMake(0.0f, 0.0f, frame.size.width, frame.size.height));	
	} else {
		CGContextAddRect(context, CGRectMake(0.0f, 0.0f, frame.size.width, 25.0f));
	}
	CGContextDrawPath(context, kCGPathStroke);	
	CGContextMoveToPoint(context, 0.0f, 25.0f);
	CGContextAddLineToPoint(context, frame.size.width, 25.0f);
	CGContextMoveToPoint(context, frame.size.width - 25, 0);
	CGContextAddLineToPoint(context, frame.size.width - 25, 25.0f);
	
	CGContextStrokePath(context);
}

#pragma mark -
#pragma mark drawRect methods

- (void)drawRect:(CGRect)rect {
	[self drawListFrameWithFrame:self.frame withContext:UIGraphicsGetCurrentContext()];
}

#pragma mark -
#pragma mark dealloc memery methods

- (void)dealloc {
	comboBoxTableView.delegate = nil;
	comboBoxTableView.dataSource = nil;
	
	[comboBoxDatasource	release];
	comboBoxDatasource = nil;
	
    [super dealloc];
}

@end

 

效果图:


分享到:
评论

相关推荐

    DataGridView列头添加下拉列表实现数据过滤

    标题“DataGridView列头添加下拉列表实现数据过滤”涉及的核心知识点包括: 1. **自定义`DataGridView`列头单元格**:在标准的`DataGridViewTextBoxColumn`或`DataGridViewButtonColumn`等列类型无法满足需求时,...

    安卓--级联下拉列表实现DEMO

    这个"安卓--级联下拉列表实现DEMO"是一个实例,它展示了如何在Android应用中创建并使用这种交互效果。下面将详细介绍这个DEMO中的关键知识点。 首先,我们来看看级联下拉列表的基本概念。级联意味着一个下拉列表的...

    年月日下拉列表实现

    "年月日下拉列表实现"是一个常见且实用的UI元素,广泛应用于各种应用程序,如表单填写、日历插件、事件计划等。这个功能允许用户在下拉菜单中选择特定的日期,简化了输入过程,减少了错误输入的可能性。 在描述中...

    给DataGridView表头添加下拉列表实现数据过滤

    以上就是给DataGridView表头添加下拉列表实现数据过滤的基本步骤和关键知识点。通过这种方式,用户可以更直观地筛选数据,提升应用的用户体验。在实际项目中,可以根据具体需求进行调整和优化,确保功能的稳定性和...

    jquery联动下拉列表实现

    "jquery联动下拉列表实现"是jQuery在实际应用中的一个常见功能,通常用于多级选择或者筛选场景,例如地区选择、产品分类等。这个功能可以让用户在选择一个下拉列表选项后,自动更新另一个下拉列表的内容,提高用户...

    Android仿微信下拉列表实现(加顶部菜单栏和底部菜单栏)

    综上所述,实现“Android仿微信下拉列表实现(加顶部菜单栏和底部菜单栏)”需要结合`SwipeRefreshLayout`、`RecyclerView`、`Toolbar`、`BottomNavigationView`、`TabLayout`和`ViewPager`等多个组件。通过合理配置...

    iphone下拉列表实现

    在iOS开发中,实现iPhone的下拉列表通常涉及到UITableView或者UICollectionView控件的使用。这些控件是Apple提供的原生UI组件,能够展示一系列可滚动的数据项,非常适合构建类似下拉菜单的效果。本教程将深入探讨...

    动态下拉列表实现dhtmlxCombo

    在本文中,我们将深入探讨如何使用dhtmlxCombo来实现动态下拉列表,以及它的一些关键功能和使用场景。 首先,dhtmlxCombo是一个轻量级的JavaScript组件,支持多种浏览器环境,包括Chrome、Firefox、Safari、IE等。...

    jquery简单实现级联下拉列表

    在给定的代码片段中,我们看到一个简单的级联下拉列表实现。页面中有两个下拉列表,`#s1` 和 `#s2`。`#s1` 的改变会触发 `change()` 函数,该函数根据 `#s1` 的选中值动态修改 `#s2` 的内容。 #### 代码解析 1. **...

    C#WinForm中DataGridView表头下拉列表

    总的来说,通过自定义`DataGridViewTextBoxColumn`和`DataGridViewColumnHeaderCell`,我们可以实现C# WinForm的`DataGridView`表头下拉列表功能,提供用户友好的筛选体验。这不仅提高了程序的交互性,也为处理大量...

    c#可输入的下拉列表框

    "C#可输入的下拉列表框"是一种交互式控件,它结合了传统的下拉列表功能和文本输入框的功能,允许用户既可以从中选择已有的选项,也可以自由输入新的值。这种控件在提高用户体验和数据输入灵活性方面起到了重要作用。...

    LabVIEW实现下拉列表菜单

    本教程将详细讲解如何使用LabVIEW实现下拉列表菜单及其功能。 首先,下拉列表控件在LabVIEW中的图标是一个小三角形,通常与一个文本框一起出现。当用户点击三角形时,会弹出一个菜单,展示所有可选的项目。这种控件...

    Extjs复选下拉列表实现了全选全不选和操作序自然序功能

    主要实现下拉列表复选功能,从Ext.ux.form.LovCombo.js文件改进而来 目前实现的有全选,全不选,自然序,操作序功能 changeSort负责实现操作序功能(即先选的显示顺序在前,后选的在后;取消操作也不影响操作的顺序...

    下拉列表模糊搜索

    在本篇中,我们将深入探讨“模糊搜索”在下拉列表中的实现原理、常见技术和应用实例。 首先,我们要理解模糊搜索的基本概念。模糊搜索,也称为部分匹配或通配符搜索,它允许用户在输入时不必完全输入目标内容,只需...

    树形下拉列表框

    ### 树形下拉列表实现 实现树形下拉列表通常有以下步骤: 1. **HTML结构**:首先,你需要创建一个基础的`&lt;select&gt;`元素,然后在其中插入`&lt;optgroup&gt;`元素来表示层级。每个`&lt;optgroup&gt;`代表一个父节点,`&lt;option&gt;`...

    Android中的下拉列表

    首先,我们要了解Android中的两种主要下拉列表实现方式:Spinner和DropDownListView。Spinner是Android SDK内置的一个控件,它可以显示一个下拉菜单,当用户点击时会弹出选项列表。而DropDownListView则通常结合...

    级联下拉列表

    这是一个基础的级联下拉列表实现,对于更复杂的需求,例如异步加载数据、多级级联等,可能需要引入更高级的库,如jQuery UI的`selectmenu`插件或现代前端框架(如React、Vue、Angular)中的组件库。不过,这个简单的...

    下拉列表的实现20190108_Objective-C_ios_OC语言_下拉列表_

    Objective-C作为苹果官方支持的iOS开发语言之一,提供了一些方法来实现下拉列表的效果。以下我们将详细探讨如何在Objective-C中实现这一功能。 首先,我们需要了解iOS中的两种主要控件,它们可以用于模拟下拉列表的...

    在datagridview控件实现下拉列表

    本篇文章将详细讲解如何在DataGridView控件中实现下拉列表功能。 首先,你需要创建一个DataGridView控件并将其绑定到数据源。这可以是数据库、数组或其他数据集。例如,你可能使用以下代码创建和绑定控件: ```...

Global site tag (gtag.js) - Google Analytics