效果
// Commbox.h
// CommboxDemo
//
// Created by xuqiang on 13-9-27.
// Copyright (c) 2013年 xuqiang. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface Commbox : UIView<UITableViewDataSource, UITableViewDelegate,UITextFieldDelegate>
{
UITableView *tv;//下拉列表
NSMutableArray *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
</pre><br><br>
[html] view plaincopy
<pre name="code" class="html">//
// Commbox.m
// CommboxDemo
//
// Created by xuqiang on 13-9-27.
// Copyright (c) 2013年 xuqiang. All rights reserved.
//
#import "Commbox.h"
@implementation Commbox
@synthesize tv,tableArray,textField;
- (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) {
// Initialization code
//textField.delegate = self;
}
if(self){
showList = NO; //默认不显示下拉框
tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width + 80 , 0)];
tv.delegate = self;
tv.dataSource = self;
tv.backgroundColor = [UIColor grayColor];
tv.separatorColor = [UIColor lightGrayColor];
tv.hidden = YES;
[self addSubview:tv];
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width + 80, 30)];
textField.font = [UIFont systemFontOfSize:8.0f];
//textField.userInteractionEnabled = NO;
textField.borderStyle=UITextBorderStyleRoundedRect;//设置文本框的边框风格
[textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
[self addSubview:textField];
}
return self;
}
//- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyBoBoardHidden:) name:UIKeyboardWillShowNotification object:nil];
// return YES;
//}
//
//- (void)keyBoBoardHidden:(NSNotification *)Notification{
// //[self.textField resignFirstResponder];
// return;
//}
-(void)dropdown{
[textField resignFirstResponder];
if (showList) {//如果下拉框已显示,什么都不做
return;
}else {//如果下拉框尚未显示,则进行显示
CGRect sf = self.frame;
sf.size.height = frameHeight;
//把dropdownList放到前面,防止下拉框被别的控件遮住
[self.superview bringSubviewToFront:self];
tv.hidden = NO;
showList = YES;//显示下拉框
CGRect frame = tv.frame;
frame.size.height = 0;
tv.frame = frame;
frame.size.height = tabheight;
[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
self.frame = sf;
tv.frame = frame;
[UIView commitAnimations];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [tableArray count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] ;
}
cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];
cell.textLabel.font = [UIFont systemFontOfSize:8.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);
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
@end
</pre><pre name="code" class="html"></pre><pre name="code" class="html">下面是添加代码</pre><pre name="code" class="html"><pre name="code" class="html">//
// ViewController.m
// CommboxDemo
//
// Created by xuqiang on 13-9-27.
// Copyright (c) 2013年 xuqiang. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//*****************************************************************************************************************
NSMutableDictionary *mulDic = [NSMutableDictionary dictionary];
[mulDic setObject:[NSArray arrayWithObjects:@"15000000", @"/MHz" , nil] forKey:@"蜂窝公众通信(全国网)"];
[mulDic setObject:[NSArray arrayWithObjects:@"15000000", @"/MHz" , nil] forKey:@"蜂窝公众通信(非全国网)"];
[mulDic setObject:[NSArray arrayWithObjects:@"50000", @"每频点" , nil] forKey:@"集群无线调度系统(全国范围使用)"];
[mulDic setObject:[NSArray arrayWithObjects:@"10000", @"每频点" , nil] forKey:@"集群无线调度系统(省、自治区、直辖市范围使用)"];
[mulDic setObject:[NSArray arrayWithObjects:@"2000", @"每频点" , nil] forKey:@"集群无线调度系统(地、市范围使用)"];
[mulDic setObject:[NSArray arrayWithObjects:@"2000000", @"每频点" , nil] forKey:@"无线寻呼系统(全国范围使用)"];
[mulDic setObject:[NSArray arrayWithObjects:@"2000000", @"每频点" , nil] forKey:@"无线寻呼系统(省、自治区、直辖市范围使用"];
[mulDic setObject:[NSArray arrayWithObjects:@"40000", @"每频点" , nil] forKey:@"无线寻呼系统(地、市范围使用)"];
[mulDic setObject:[NSArray arrayWithObjects:@"150", @"每基站" , nil] forKey:@"无绳电话系统"];
arrayData = [mulDic allKeys];
//*****************************************************************************************************************
_commbox = [[Commbox alloc] initWithFrame:CGRectMake(70, 10, 140, 100)];
_commbox.textField.placeholder = @"点击请选择";
NSMutableArray* arr = [[NSMutableArray alloc] init];
//*****************************************************************************************************************
NSEnumerator *e = [mulDic keyEnumerator];
for (NSString *key in e) {
//NSLog(@"Key is %@, value is %@", key, [mulDic objectForKey:key]);
[arr addObject:key];
}
//NSLog(@"%@",[arr objectAtIndex:0]);
//*****************************************************************************************************************
_commbox.tableArray = arr;
[self.view addSubview:_commbox];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}