iOS------下拉框

#import .h

@interface ListTableView : UIView

@property (nonatomic, strong) NSMutableArray* listTableArr;//下拉列表数据

@property (nonatomic, copy) NSString* textPlaceholder ;//文本占位符

@property (nonatomic, strong)UIColor* placeholderColor;//占位符的颜色

@property (nonatomic, strong)UIColor* textColor;//文本颜色

@property (nonatomic, strong)UIColor* textBackgroundColor;//文本背景颜色

@property (nonatomic, assign)CGFloat textFont;//文本字体大小

@property (nonatomic, assign)CGFloat textHeight;//文本高度---最终显示的高度--默认30

@end

--------------------------------------------------------------

#import "ListTableView.h"

@interface ListTableView(){

BOOL showList;//是否弹出下拉列表

CGFloat tableListHeight;//下拉列表的高度

CGFloat frameHeight;//frame的高度

}

@property (nonatomic, strong) UITableView* listTable;//下拉列表

@property (nonatomic,strong) UILabel* textLabel;//文本输入框

@end

@implementation ListTableView

- (void)setTextPlaceholder:(NSString *)textPlaceholder{

_textPlaceholder = textPlaceholder;

_textLabel.text = _textPlaceholder;

}

- (void)setPlaceholderColor:(UIColor *)placeholderColor{

_placeholderColor = placeholderColor;

_textLabel.textColor = placeholderColor;

}

- (void)setTextFont:(CGFloat)textFont{

_textFont = textFont;

_textFont = _textFont>0? _textFont:18.0f;

_textLabel.font = [UIFont systemFontOfSize:_textFont];

}

- (void)setTextColor:(UIColor *)textColor{

_textColor = textColor;

_textLabel.textColor =textColor;

}

- (void)setTextBackgroundColor:(UIColor *)textBackgroundColor{

_textBackgroundColor = textBackgroundColor;

_textLabel.backgroundColor  = textBackgroundColor;

}

- (void)setTextHeight:(CGFloat)textHeight{

_textHeight = textHeight;

[self initFrame];

}

- (id)initWithFrame:(CGRect)frame

{

self = [super initWithFrame:frame];

if (self) {

self.backgroundColor = [UIColor whiteColor];

showList = NO;//默认不显示下拉框

_listTable = [[UITableView alloc]init];

_listTable.delegate = self;

_listTable.dataSource = self;

_listTable.backgroundColor = [UIColor lightGrayColor];

_listTable.separatorColor = [UIColor lightGrayColor];

_listTable.hidden = YES;

_listTable.tableHeaderView.frame = CGRectZero;

_listTable.tableFooterView.frame = CGRectZero;

[self addSubview:_listTable];

_textLabel = [[UILabel alloc]init];

[self addSubview:_textLabel];

_textLabel.textColor = [UIColor blackColor];

_textLabel.backgroundColor = [UIColor lightGrayColor] ;

_textLabel.textAlignment = NSTextAlignmentCenter;

UITapGestureRecognizer* tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dropdown)];

_textLabel.userInteractionEnabled = YES;

[_textLabel addGestureRecognizer:tap];

}

return self;

}

- (void)layoutSubviews{

[super layoutSubviews];

[self initFrame];

}

- (void)initFrame{

_textHeight = _textHeight>0? _textHeight:30;

if (self.frame.size.height <200) {

frameHeight = 200;

}else{

frameHeight = self.frame.size.height;

}

tableListHeight = frameHeight-_textHeight;

_textLabel.frame = CGRectMake(0, self.bounds.origin.y,  self.frame.size.width, _textHeight);

_listTable.frame = CGRectMake(0, _textLabel.frame.origin.y+_textLabel.frame.size.height, self.frame.size.width, tableListHeight);

}

- (void)dropdown{

if (showList) {//如果下拉框以显示,什么都不做

return;

}else{//如果下拉框尚未显示,则进行显示

CGRect selfFrame = self.frame;

selfFrame.size.height = frameHeight;

[self.superview bringSubviewToFront:self];//把下拉框放到最前面,防止下拉框被别的控件遮住

_listTable.hidden = NO;

showList = YES;//显示下拉框

CGRect TvFrame = _listTable.frame;

TvFrame.size.height = self.listTableArr.count*35;

_listTable.frame = TvFrame;

TvFrame.size.height = tableListHeight;

[UIView beginAnimations:@"ResizeForKeyBoard" context:nil];

[UIView setAnimationCurve:UIViewAnimationCurveLinear];

self.frame = selfFrame;

_listTable.frame = TvFrame;

[UIView commitAnimations];

}

}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

{

return 1;

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return [_listTableArr 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 = [_listTableArr objectAtIndex:[indexPath row]];

cell.textLabel.textColor = _textColor !=nil? _textColor:[UIColor blackColor];

cell.textLabel.font = [UIFont systemFontOfSize:_textFont];

cell.accessoryType  = UITableViewCellAccessoryNone;

cell.selectionStyle = UITableViewCellSelectionStyleNone;

return cell;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

return 35;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

_textPlaceholder = [_listTableArr objectAtIndex:indexPath.row];

_textLabel.text = _textPlaceholder;

_textLabel.textColor = _textColor;

showList = NO;

_listTable.hidden = YES;

CGRect selfFrame = self.frame;

selfFrame.size.height = _textHeight;

self.frame = selfFrame;

CGRect tvFrame = _listTable.frame;

tvFrame.size.height = 0;

_listTable.frame = tvFrame;

}

@end

---------------------------------------------

具体用法:

self.automaticallyAdjustsScrollViewInsets = NO;//防止ListTableView页面下垂

ListTableView* listView = [[ListTableView alloc]init];

[self.view addSubview:listView];

listView.frame = CGRectMake(70, 120, 140, 80);//listView的高度在不设置textHeight的情况下,需大于等于30;设置textHeight的话,需大于等于textHeight

listView.listTableArr = [@[@"1",@"2",@"3",@"5",@"6",@"7"]mutableCopy];

[listView.listTableArr  addObject:@"4"];

listView.textColor = [UIColor redColor];//也可不设置,会有默认的

listView.placeholderColor = [UIColor yellowColor];//也可不设置,会有默认的

listView.textBackgroundColor = [UIColor lightGrayColor];//也可不设置,会有默认的

listView.textPlaceholder  = @"点击请选择";

listView.textFont = 12.0f;//也可不设置,会有默认的

listView.textHeight = 50;//也可不设置,会有默认30

你可能感兴趣的:(iOS------下拉框)