iOS------下拉框

#import <UIKit/UIKit>.h

@interface ListTableView : UIView

@property (nonatomic, strong) NSMutableArray* listTableArr;//下拉列表數(shù)據(jù)

@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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市蓖议,隨后出現(xiàn)的幾起案子虏杰,更是在濱河造成了極大的恐慌,老刑警劉巖勒虾,帶你破解...
    沈念sama閱讀 217,826評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件纺阔,死亡現(xiàn)場離奇詭異,居然都是意外死亡从撼,警方通過查閱死者的電腦和手機州弟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,968評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來低零,“玉大人婆翔,你說我怎么就攤上這事√蜕簦” “怎么了啃奴?”我有些...
    開封第一講書人閱讀 164,234評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長雄妥。 經常有香客問我最蕾,道長依溯,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,562評論 1 293
  • 正文 為了忘掉前任瘟则,我火速辦了婚禮黎炉,結果婚禮上,老公的妹妹穿的比我還像新娘醋拧。我一直安慰自己慷嗜,他們只是感情好,可當我...
    茶點故事閱讀 67,611評論 6 392
  • 文/花漫 我一把揭開白布丹壕。 她就那樣靜靜地躺著庆械,像睡著了一般。 火紅的嫁衣襯著肌膚如雪菌赖。 梳的紋絲不亂的頭發(fā)上缭乘,一...
    開封第一講書人閱讀 51,482評論 1 302
  • 那天,我揣著相機與錄音琉用,去河邊找鬼堕绩。 笑死,一個胖子當著我的面吹牛辕羽,可吹牛的內容都是我干的逛尚。 我是一名探鬼主播,決...
    沈念sama閱讀 40,271評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼刁愿,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了到逊?” 一聲冷哼從身側響起铣口,我...
    開封第一講書人閱讀 39,166評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎觉壶,沒想到半個月后脑题,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,608評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡铜靶,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,814評論 3 336
  • 正文 我和宋清朗相戀三年叔遂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片争剿。...
    茶點故事閱讀 39,926評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡已艰,死狀恐怖,靈堂內的尸體忽然破棺而出蚕苇,到底是詐尸還是另有隱情哩掺,我是刑警寧澤,帶...
    沈念sama閱讀 35,644評論 5 346
  • 正文 年R本政府宣布涩笤,位于F島的核電站嚼吞,受9級特大地震影響盒件,放射性物質發(fā)生泄漏。R本人自食惡果不足惜舱禽,卻給世界環(huán)境...
    茶點故事閱讀 41,249評論 3 329
  • 文/蒙蒙 一炒刁、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧誊稚,春花似錦切心、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,866評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至俏脊,卻和暖如春全谤,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背爷贫。 一陣腳步聲響...
    開封第一講書人閱讀 32,991評論 1 269
  • 我被黑心中介騙來泰國打工认然, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人漫萄。 一個月前我還...
    沈念sama閱讀 48,063評論 3 370
  • 正文 我出身青樓卷员,卻偏偏與公主長得像,于是被迫代替她去往敵國和親腾务。 傳聞我的和親對象是個殘疾皇子毕骡,可洞房花燭夜當晚...
    茶點故事閱讀 44,871評論 2 354

推薦閱讀更多精彩內容