iOS 下拉列表
来源:互联网 发布:sql中建立表的命令是 编辑:程序博客网 时间:2024/06/06 05:13
1.声明 DropDown : UIView
#import <UIKit/UIKit.h>
@interface DropDown :UIView<UITableViewDataSource,UITableViewDelegate>{
BOOL showlist;
CGFloat tabHeight;
CGFloat frameHeight;
}
@property(nonatomic,strong)UITableView *tv;
@property(nonatomic,strong)NSArray *tableArray;
@property(nonatomic,strong)UITextField *textFeild;
@end
#import "DropDown.h"
@implementation DropDown
-(id)initWithFrame:(CGRect)frame{
if(frame.size.height<200){
frameHeight=200;
}else{
frameHeight=frame.size.height;
}
tabHeight=frameHeight-30;
frame.size.height=30.f;
self=[superinitWithFrame:frame];
if(self){
showlist=NO;
self.tv = [[UITableViewalloc] initWithFrame:CGRectMake(0,30, frame.size.width,0)];
self.tv.delegate =self;
self.tv.dataSource =self;
self.tv.backgroundColor = [UIColorgrayColor];
self.tv.separatorColor = [UIColorlightGrayColor];
self.tv.hidden =YES;
[self addSubview:self.tv];
self.textFeild = [[UITextFieldalloc] initWithFrame:CGRectMake(0,0, frame.size.width,30)];
self.textFeild.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格
[self.textFeildaddTarget:selfaction:@selector(dropdown)forControlEvents:UIControlEventAllTouchEvents];
[self addSubview:self.textFeild];
}
return self;
}
-(void)dropdown{
[self.textFeildresignFirstResponder];
if (showlist) {//如果下拉框已显示,什么都不做
return;
}else {//如果下拉框尚未显示,则进行显示
CGRect sf = self.frame;
sf.size.height =frameHeight;
//把dropdownList放到前面,防止下拉框被别的控件遮住
[self.superviewbringSubviewToFront:self];
self.tv.hidden =NO;
showlist = YES;//显示下拉框
CGRect frame =self.tv.frame;
frame.size.height =0;
self.tv.frame = frame;
frame.size.height =tabHeight;
[UIViewbeginAnimations:@"ResizeForKeyBoard"context:nil];
[UIViewsetAnimationCurve:UIViewAnimationCurveLinear];
self.frame = sf;
self.tv.frame = frame;
[UIViewcommitAnimations];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.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];
}
cell.textLabel.text = [self.tableArrayobjectAtIndex:[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{
self.textFeild.text = [self.tableArrayobjectAtIndex:[indexPath row]];
showlist = NO;
self.tv.hidden =YES;
CGRect sf = self.frame;
sf.size.height =30;
self.frame = sf;
CGRect frame = self.tv.frame;
frame.size.height =0;
self.tv.frame = frame;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
2.使用
DropDown *dd1 = [[DropDownalloc] initWithFrame:CGRectMake(10,10, 140, 100)];
dd1.textFeild.placeholder =@"请输入内容";
NSArray* arr=[[NSArrayalloc]initWithObjects:@"A",@"B",@"C",@"D",@"E",@"F",nil];
dd1.tableArray = arr;
[self.viewaddSubview:dd1];
- iOS 创建下拉列表
- iOS下拉列表
- ios自定义下拉列表
- iOS开发 下拉列表
- iOS 下拉列表
- ios QQ下拉列表 UITableViewHeaderFooterView
- iOS UITableView中的下拉列表
- 【IOS功能实现】之:下拉列表
- iOS 点击按钮出现一个下拉列表
- iOS study Day18-下拉列表框
- IOS UITableView Section下拉列表实现
- IOS 列表头视图下拉放大
- iOS 简单实现下拉列表框
- 【IOS功能实现】之:下拉列表
- iOS源码学习-EGORefreshTableHeaderView下拉列表更新列表内容
- 下拉列表
- 下拉列表
- 下拉列表
- C++实验四—乘法口诀表
- spring框架学习(六)AOP
- c++上机实验---四
- listFiles返回null
- UITbaleView滚动到最后一行
- iOS 下拉列表
- 第六周项目一
- matlab中当前文件目录和搜索路径的区别
- c++实验4-百钱白鸡问题
- 数据结构与算法01--堆栈 & 队列
- Android 高仿微信6.0主界面 带你玩转切换图标变色
- Struts2框架学习(一)
- 【bzoj2666】【cqoi2012】【组装】【贪心】
- c++实验4 项目一 1000以内所有偶数之和