IOS开发之下拉列表控件开发

本文介绍了一种自定义下拉列表控件的实现方式,通过创建`Commbox`类,集成`UITableViewDataSource`、`UITableViewDelegate`和`UITextFieldDelegate`协议,实现了在iOS应用中显示下拉列表的功能。代码示例详细展示了如何初始化和操作这个自定义控件,包括列表的显示和隐藏、数据源处理、单元格定制等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

效果

[html]  view plain copy
  1. //  Commbox.h    
  2. //  CommboxDemo    
  3. //    
  4. //  Created by xuqiang on 13-9-27.    
  5. //  Copyright (c) 2013年 xuqiang. All rights reserved.    
  6. //    
  7.     
  8. #import <UIKit/UIKit.h>    
  9.     
  10. @interface Commbox : UIView<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>    
  11. {    
  12.     UITableView *tv;//下拉列表    
  13.     NSMutableArray *tableArray;//下拉列表数据    
  14.     UITextField *textField;//文本输入框    
  15.     BOOL showList;//是否弹出下拉列表    
  16.     CGFloat tabheight;//table下拉列表的高度    
  17.     CGFloat frameHeight;//frame的高度    
  18. }    
  19.     
  20. @property (nonatomic,retain) UITableView *tv;    
  21. @property (nonatomic,retain) NSArray *tableArray;    
  22. @property (nonatomic,retain) UITextField *textField;    
  23.     
  24. @end    
  25. </pre><br><br>    
  26. [html] view plaincopy  
  27. <pre name="code" class="html">//    
  28. //  Commbox.m    
  29. //  CommboxDemo    
  30. //    
  31. //  Created by xuqiang on 13-9-27.    
  32. //  Copyright (c) 2013年 xuqiang. All rights reserved.    
  33. //    
  34.     
  35. #import "Commbox.h"    
  36.     
  37. @implementation Commbox    
  38. @synthesize tv,tableArray,textField;    
  39.     
  40. - (id)initWithFrame:(CGRect)frame    
  41. {    
  42.         
  43.     if (frame.size.height<200) {    
  44.         frameHeight = 200;    
  45.     }else{    
  46.         frameHeight = frame.size.height;    
  47.     }    
  48.     tabheight = frameHeight-30;    
  49.         
  50.     frame.size.height = 30.0f;    
  51.         
  52.     self = [super initWithFrame:frame];    
  53.     if (self) {    
  54.         // Initialization code    
  55.         //textField.delegate = self;    
  56.     }    
  57.         
  58.     if(self){    
  59.         showList = NO; //默认不显示下拉框    
  60.             
  61.         tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width + 80 , 0)];    
  62.         tv.delegate = self;    
  63.         tv.dataSource = self;    
  64.         tv.backgroundColor = [UIColor grayColor];    
  65.         tv.separatorColor = [UIColor lightGrayColor];    
  66.         tv.hidden = YES;    
  67.         [self addSubview:tv];    
  68.             
  69.         textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width + 80, 30)];    
  70.         textField.font = [UIFont systemFontOfSize:8.0f];    
  71.         //textField.userInteractionEnabled = NO;    
  72.         textField.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格    
  73.         [textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];    
  74.         [self addSubview:textField];    
  75.             
  76.     }    
  77.     return self;    
  78. }    
  79.     
  80. //- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {    
  81. ////    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoBoardHidden:) name:UIKeyboardWillShowNotification object:nil];    
  82. //    return YES;    
  83. //}    
  84. //    
  85. //- (void)keyBoBoardHidden:(NSNotification *)Notification{    
  86. //    //[self.textField resignFirstResponder];    
  87. //    return;    
  88. //}    
  89.     
  90. -(void)dropdown{    
  91.     [textField resignFirstResponder];    
  92.     if (showList) {//如果下拉框已显示,什么都不做    
  93.         return;    
  94.     }else {//如果下拉框尚未显示,则进行显示    
  95.             
  96.         CGRect sf = self.frame;    
  97.         sf.size.height = frameHeight;    
  98.             
  99.         //把dropdownList放到前面,防止下拉框被别的控件遮住    
  100.         [self.superview bringSubviewToFront:self];    
  101.         tv.hidden = NO;    
  102.         showList = YES;//显示下拉框    
  103.             
  104.         CGRect frame = tv.frame;    
  105.         frame.size.height = 0;    
  106.         tv.frame = frame;    
  107.         frame.size.height = tabheight;    
  108.         [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];    
  109.         [UIView setAnimationCurve:UIViewAnimationCurveLinear];    
  110.         self.frame = sf;    
  111.         tv.frame = frame;    
  112.         [UIView commitAnimations];    
  113.     }    
  114. }    
  115.     
  116. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView    
  117. {    
  118.     return 1;    
  119. }    
  120.     
  121. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section    
  122. {    
  123.     return [tableArray count];    
  124. }    
  125.     
  126. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath    
  127. {    
  128.     static NSString *CellIdentifier = @"Cell";    
  129.         
  130.     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];    
  131.     if (cell == nil) {    
  132.         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;    
  133.     }    
  134.         
  135.     cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];    
  136.     cell.textLabel.font = [UIFont systemFontOfSize:8.0f];    
  137.     cell.accessoryType  = UITableViewCellAccessoryNone;    
  138.     cell.selectionStyle = UITableViewCellSelectionStyleGray;    
  139.         
  140.     return cell;    
  141. }    
  142. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath    
  143. {    
  144.     return 35;    
  145. }    
  146. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath    
  147. {    
  148.     textField.text = [tableArray objectAtIndex:[indexPath row]];    
  149.     showList = NO;    
  150.     tv.hidden = YES;    
  151.         
  152.     CGRect sf = self.frame;    
  153.     sf.size.height = 30;    
  154.     self.frame = sf;    
  155.     CGRect frame = tv.frame;    
  156.     frame.size.height = 0;    
  157.     tv.frame = frame;    
  158. }    
  159.     
  160. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation    
  161. {    
  162.     // Return YES for supported orientations    
  163.     return (interfaceOrientation == UIInterfaceOrientationPortrait);    
  164. }    
  165. /*    
  166. // Only override drawRect: if you perform custom drawing.    
  167. // An empty implementation adversely affects performance during animation.    
  168. - (void)drawRect:(CGRect)rect    
  169. {    
  170.     // Drawing code    
  171. }    
  172. */    
  173.     
  174. @end    
  175. </pre><pre name="code" class="html"></pre><pre name="code" class="html">下面是添加代码</pre><pre name="code" class="html"><pre name="code" class="html">//    
  176. //  ViewController.m    
  177. //  CommboxDemo    
  178. //    
  179. //  Created by xuqiang on 13-9-27.    
  180. //  Copyright (c) 2013年 xuqiang. All rights reserved.    
  181. //    
  182.     
  183. #import "ViewController.h"    
  184.     
  185. @interface ViewController ()    
  186.     
  187. @end    
  188.     
  189. @implementation ViewController    
  190.     
  191. - (void)viewDidLoad    
  192. {    
  193.     [super viewDidLoad];    
  194.     // Do any additional setup after loading the view, typically from a nib.    
  195.     //*****************************************************************************************************************    
  196.     NSMutableDictionary *mulDic = [NSMutableDictionary dictionary];    
  197.     [mulDic setObject:[NSArray arrayWithObjects:@"15000000", @"/MHz"    , nil] forKey:@"蜂窝公众通信(全国网)"];    
  198.     [mulDic setObject:[NSArray arrayWithObjects:@"15000000", @"/MHz"    , nil] forKey:@"蜂窝公众通信(非全国网)"];    
  199.     [mulDic setObject:[NSArray arrayWithObjects:@"50000",    @"每频点"   , nil] forKey:@"集群无线调度系统(全国范围使用)"];    
  200.     [mulDic setObject:[NSArray arrayWithObjects:@"10000",    @"每频点"   , nil] forKey:@"集群无线调度系统(省、自治区、直辖市范围使用)"];    
  201.     [mulDic setObject:[NSArray arrayWithObjects:@"2000",     @"每频点"   , nil] forKey:@"集群无线调度系统(地、市范围使用)"];    
  202.     [mulDic setObject:[NSArray arrayWithObjects:@"2000000",  @"每频点"   , nil] forKey:@"无线寻呼系统(全国范围使用)"];    
  203.     [mulDic setObject:[NSArray arrayWithObjects:@"2000000",  @"每频点"   , nil] forKey:@"无线寻呼系统(省、自治区、直辖市范围使用"];    
  204.     [mulDic setObject:[NSArray arrayWithObjects:@"40000",    @"每频点"   , nil] forKey:@"无线寻呼系统(地、市范围使用)"];    
  205.     [mulDic setObject:[NSArray arrayWithObjects:@"150",      @"每基站"   , nil] forKey:@"无绳电话系统"];    
  206.      
  207.     arrayData = [mulDic allKeys];    
  208.     //*****************************************************************************************************************    
  209.        
  210.     _commbox = [[Commbox alloc] initWithFrame:CGRectMake(70, 10, 140, 100)];    
  211.     _commbox.textField.placeholder = @"点击请选择";    
  212.         
  213.     NSMutableArray* arr = [[NSMutableArray alloc] init];    
  214.     //*****************************************************************************************************************    
  215.     NSEnumerator *e = [mulDic keyEnumerator];    
  216.     for (NSString *key in e) {    
  217.         //NSLog(@"Key is %@, value is %@", key, [mulDic objectForKey:key]);    
  218.        [arr addObject:key];    
  219.             
  220.     }    
  221.     //NSLog(@"%@",[arr objectAtIndex:0]);    
  222.     //*****************************************************************************************************************    
  223.     
  224.     _commbox.tableArray = arr;    
  225.     
  226.     [self.view addSubview:_commbox];    
  227.      
  228.     
  229. }    
  230.     
  231. - (void)didReceiveMemoryWarning    
  232. {    
  233.     [super didReceiveMemoryWarning];    
  234.     // Dispose of any resources that can be recreated.    
  235. }    


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值