很多时候我们都会用到下拉列表框,闲着没事自己实现了一个共享之。
代码如下:
//
// CLOCombobox.h
// CLOComboboxText
//
// Created by Cloay on 12-8-9.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface CLOCombobox : UIView<UITableViewDelegate, UITableViewDataSource>{
NSArray *array;
UITextField *textField;
UITableView *comboboxItems;
}
- (id)initWithFrame:(CGRect)frame withItems:(NSArray *)items withSuperView:(UIView *)view;
@end
//
// CLOCombobox.m
// CLOComboboxText
//
// Created by Cloay on 12-8-9.
// Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//
#import "CLOCombobox.h"
#define itemHeight 35
#define tableviewHeight 180
@implementation CLOCombobox
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
- (id)initWithFrame:(CGRect)frame withItems:(NSArray *)items withSuperView:(UIView *)view{
self = [super initWithFrame:frame];
[self setBackgroundColor:[UIColor clearColor]];
//文本框
textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 4, frame.size.width, frame.size.height - 4)];
[textField setBorderStyle:UITextBorderStyleRoundedRect];
[textField setBackgroundColor:[UIColor whiteColor]];
[textField setEnabled:NO];
// [textField setB]
// [textField setTextAlignment:UITextAlignmentCenter];
//默认显示0处的文字
textField.text = [items objectAtIndex:0];
[self addSubview:textField];
//下拉按钮
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(4*frame.size.width/5, 0, frame.size.height, frame.size.height)];
[button setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonDidPressed:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:button];
array = [items mutableCopy];
comboboxItems = [[UITableView alloc] initWithFrame:CGRectMake(frame.origin.x, frame.origin.y + frame.size.height, self.frame.size.width, tableviewHeight) style:UITableViewStyleGrouped];
comboboxItems.bounces = NO;
comboboxItems.delegate = self;
comboboxItems.dataSource = self;
comboboxItems.hidden = YES;
[comboboxItems setBackgroundColor:[UIColor clearColor]];
[view addSubview:comboboxItems];
return self;
}
- (void)buttonDidPressed:(id)sender{
comboboxItems.hidden = NO;
}
#pragma mark -
#pragma mark - table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return [array count];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return 1;
}
- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
return itemHeight;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = @"CELL";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
[cell.textLabel setFont:[UIFont systemFontOfSize:14]];
[cell setSelectionStyle:UITableViewCellEditingStyleNone];
}
cell.textLabel.text = [array objectAtIndex:[indexPath row]];
return cell;
}
#pragma mark - table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
NSLog(@"row - >%i", [indexPath row]);
//选中后显示
textField.text = [array objectAtIndex:[indexPath row]];
comboboxItems.hidden = YES;
}
@end
代码比较简单,就不过多解释了。