`
zjjzmw1
  • 浏览: 1352678 次
  • 性别: Icon_minigender_1
  • 来自: 开封
社区版块
存档分类
最新评论

ios学习--iphone 实现下拉菜单

    博客分类:
  • iOS
 
阅读更多

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];

 
分享到:
评论

相关推荐

    (0031)-iOS/iPhone/iPAD/iPod源代码-列表(Table)-comboBox

    利用UITableView来实现下拉菜单的效果。 注意:请在Mac下解压使用

    (0132)-iOS/iPhone/iPAD/iPod源代码-列表(Table)-Drop Down ComboBox

    利用UITableView来实现下拉菜单(可扩展列表)的效果。点击列表某一行,会下拉显示更多行。 注意:请在Mac下解压使用

    (0135)-iOS/iPhone/iPAD/iPod源代码-列表(Table)-Retractable Section Controller

    利用UITableView来实现下拉列表(可扩展列表)的效果。下拉列表即支持列表每一行的展开与收起功能。点击列表某一行,会下拉显示更多行,再点击一次,即收起下面的所有行。 注意:请在Mac下解压使用

    iPhone下拉

    iphone 的ios4.1以下的系统,可以下拉菜单,good!行!

    jquery顶部下拉菜单插件foldit.zip

    jquery顶部下拉菜单插件foldit是一款类似iphone苹果手机ios系统顶部下拉特效代码。

    仿虾米音乐的下拉筛选菜单列表(iOS源代码)

     一个开源的 iOS 下拉筛选菜单列表, 拥有类似虾米专辑频道筛选功能, 根据服务器返回的 Tag 数据动态计算视图高度, 隐藏菜单后将会显示已选择的标签。 环境要求: 1、运行在 iOS 8.0 及以上的 iPhone/iPad。 2、...

    史上最全的ios开发源码

    原本这个系统名为iPhone OS,直到2010年6月7日WWDC大会上宣布改名为iOS。截止至2011年11月,根据Canalys的数据显示,iOS已经占据了全球智能手机系统市场份额的30%,在美国的市场占有率为43%。 源码列表: 按钮类 ...

    动态下拉菜单(iPhone源代码)

    来源:Licence:BSD (Simplified)平台:iOS设备:iPhone / ... 实现带动画效果的下拉菜单。用户按下菜单按钮,出现下拉按钮,用户松开菜单按钮,下拉按钮收回。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。

    纯swfit日历、二级下拉菜单

    文件是前端是时间用swift做app中用到的两个控件,特摘取出来供大家使用学习/ 包含两个工程, 一个是日历选择,可作为日程管理的日历控件, 另一个是仿照美团APP,...控件全部使用swift实现,兼容ios8,iphone6等版本。

    Pull Down Menu(iPhone源代码)

     实现下拉菜单功能。菜单可以通过下拉动作或者通过点击动作出现,也可以通过上拉或者点击动作隐藏。可以将下拉菜单加载NavigationConroller的导航栏上(如本demo,下拉导航栏出现菜单),或者添加在任意视图上...

    MXPullDownMenu使用方式简单的下拉菜单(iPhone源代码)

    来源:github/MXPullDownMenuLicence:MIT作者:骁骁骁骁骁同学  MXPullDownMenu是带有动画效果的下列菜单。可以选择相应的下拉菜单选项。 小编注:感谢开发者@骁骁骁骁骁同学 。

    jquery顶部下拉菜单插件foldit

    jquery顶部下拉菜单插件foldit是一款类似iphone苹果手机ios系统顶部下拉特效代码。

    jquery顶部下拉菜单插件foldit特效代码

    jquery顶部下拉菜单插件foldit是一款类似iphone苹果手机ios系统顶部下拉特效代码。

    ACNavBarDrawer(iPhone源代码)

    来源:Licence:MIT平台:iOS设备:iPhone / iPad作者:码农白腩肚  实现导航栏抽屉视图。即点击导航栏上某个按钮,会在导航栏下方下拉出现一个菜单,选择菜单上某个按钮,菜单会隐藏。 小编注:感谢开发者@...

    类似美团的下拉选项(iPhone源代码)

    来源:Licence:Unspecified作者:: 我恨、我自己。  类似于美团、大众点评的下拉菜单选项。

    Drop Down Control(iPhone源代码)

     实现下拉菜单效果。点击按钮,保持下拉菜单弹出,按住屏幕移动(press and move)选择菜单选项,松开按钮,菜单收回。下拉菜单弹出和收回的动画效果不错。 Code4App编译测试,适用环境:Xcode 4.5, iOS 6.0 以上...

    可展开的UITableViewCell(iPhone源代码)

    来源:Licence:Unspecified平台:iOS设备:... 实现可以点击下拉展开的table cell,可以用作下拉菜单等。 小编注:感谢开发者@_old_fox 分享代码于Code4App。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。

    Drop Down ComboBox(iPhone源代码)

    来源:Licence:未知平台:iOS设备:iPhone / iPad作者:Florian ... 利用UITableView来实现下拉菜单(可扩展列表)的效果。点击列表某一行,会下拉显示更多行。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。

    comboBox(iPhone源代码)

    来源:未知Licence:Unspecified平台:iOS设备:iPhone / iPad作者:  利用UITableView来实现下拉菜单的效果。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。

    网页上导航菜单效果

    源码DOPDropDownMenu,下拉菜单实现网页上导航菜单效果,这个也是我们看到的iPhone手机上的网页菜单的效果的。 测试环境:Xcode 5.0,iOS 6.0以上

Global site tag (gtag.js) - Google Analytics