iOS DropDownList-最簡單集成TextField列表選擇型控件

一個列表選擇型textfield控件息楔,只需設置控件位置并添加到所在視圖即可,用法相當簡單示例代碼如下:在要用到類的.m文件里倒入DropDown.h文件扒披,并創(chuàng)建控件全局屬性值依,在合適方法中把控件添加到控件需要在的圖層即可

用法

- (void)viewDidLoad{? ?

[super viewDidLoad];? ?

?? _dropDown = [[DropDown alloc]initWithFrame:CGRectMake(80, self.view.bounds.size.height*0.5-70,KScreenWidth-160, KScreenHeight- 200)];?

? NSArray *ar = @[@"1000",@"2000",@"3000",@"4000",@"5000",@"10000",@"20000",@"30000"];? ?

_dropDown.tableArray = ar;? ?

[self.view addSubview:_dropDown];??

}

- (void)touchesBegan:(NSSet*)touches withEvent:(UIEvent *)event {

[_dropDown resignFirstResponder];

}

DropDown控件的.h和.m文件 ------“直接粘貼即可”

DropDown.h文件


#import<UIKit/UIKit.h>

@interface DropDown:UIView<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

{

UITableView *tv;//下拉列表

NSArray *tableArray;//下拉列表數(shù)據(jù)

UITextField *textField;//文本輸入框

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

CGFloat tabheight;//table下拉列表的高度

CGFloat frameHeight;//frame的高度

}

@property (nonatomic,retain) UITableView *tv;

@property (nonatomic,retain) NSArray *tableArray;

@property (strong, nonatomic) NSDictionary *pickerDic;//獲取文件里的字典

@property (strong, nonatomic) NSArray *provinceArray;//省碟案、市

@property (nonatomic,retain) UITextField *textField;

@property (assign, nonatomic) BOOL showList;

-(void)dontshowlist;

@end

DropDown.m文件


#import "DropDown.h"

@implementation DropDown

@synthesize tv,tableArray,textField,showList;


-(id)initWithFrame:(CGRect)frame

{

if (frame.size.height<300) {

frameHeight =300;

}else{

frameHeight = frame.size.height;

}

tabheight = frameHeight-30;

frame.size.height = 30.0f;

self=[super initWithFrame:frame];

if(self){

showList = NO; //默認不顯示下拉框

tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width,300)];

tv.delegate = self;

tv.dataSource = self;

tv.separatorColor = [UIColor lightGrayColor];

tv.hidden = YES;

tv.contentOffset =CGPointMake(0, 64);

[self addSubview:tv];

textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];

textField.borderStyle=UITextBorderStyleRoundedRect;//設置文本框的邊框風格

[textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];

textField.font = [UIFont systemFontOfSize:16];

textField.placeholder = @"請選擇(長按)";

//設置textfield 不彈出鍵盤

textField.inputView=[[UIView alloc]initWithFrame:CGRectZero];

textField.backgroundColor = [UIColor colorWithRed:204/255.0 green:204/255.0 blue:204/255.0 alpha:1];

[self addSubview:textField];

NSString *path = [[NSBundle mainBundle] pathForResource:@"Address" ofType:@"plist"];

self.pickerDic = [[NSDictionary alloc] initWithContentsOfFile:path];

//省? 市

self.provinceArray = [self.pickerDic allKeys];

tableArray = self.provinceArray;

}

return self;

}

//不展示列表


-(void)dontshowlist

{

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;

}


-(void)dropdown

{

//? ? [tv reloadData];

[textField resignFirstResponder];

if (showList) {//如果下拉框已顯示愿险,什么都不做

[textField resignFirstResponder];

return;

}else {//如果下拉框尚未顯示,則進行顯示

[textField resignFirstResponder];

CGRect sf = self.frame;

sf.size.height = frameHeight;

//把dropdownList放到前面价说,防止下拉框被別的控件遮住

[self.superview bringSubviewToFront:self];

tv.hidden = NO;

showList = YES;//顯示下拉框

tv.tableHeaderView.backgroundColor = [UIColor blackColor];

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)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.backgroundColor = [UIColor grayColor];

cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];

cell.textAlignment = UITextAlignmentCenter;

cell.textLabel.font = [UIFont systemFontOfSize:16.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;

NSString *string = [NSString stringWithFormat:@"%ld",(long)[indexPath row]];

NSDictionary *dict =[[NSDictionary alloc] initWithObjectsAndKeys:string,@"textOne",nil];

//創(chuàng)建通知

NSNotification *notification =[NSNotification notificationWithName:@"tongzhi" object:nil userInfo:dict];

//通過通知中心發(fā)送通知

[[NSNotificationCenter defaultCenter] postNotification:notification];

}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation

{

// Return YES for supported orientations

return (interfaceOrientation == UIInterfaceOrientationPortrait);

}

@end

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末辆亏,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子鳖目,更是在濱河造成了極大的恐慌扮叨,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件领迈,死亡現(xiàn)場離奇詭異彻磁,居然都是意外死亡,警方通過查閱死者的電腦和手機狸捅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進店門兵迅,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人薪贫,你說我怎么就攤上這事恍箭。” “怎么了瞧省?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵扯夭,是天一觀的道長。 經(jīng)常有香客問我鞍匾,道長交洗,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任橡淑,我火速辦了婚禮构拳,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己置森,他們只是感情好斗埂,可當我...
    茶點故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著凫海,像睡著了一般呛凶。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上行贪,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天漾稀,我揣著相機與錄音,去河邊找鬼建瘫。 笑死崭捍,一個胖子當著我的面吹牛,可吹牛的內(nèi)容都是我干的啰脚。 我是一名探鬼主播缕贡,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼拣播!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起收擦,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤贮配,失蹤者是張志新(化名)和其女友劉穎轮洋,沒想到半個月后惋戏,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體于未,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨居荒郊野嶺守林人離奇死亡礼饱,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年怠苔,在試婚紗的時候發(fā)現(xiàn)自己被綠了辱士。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片玉掸。...
    茶點故事閱讀 40,498評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡伴挚,死狀恐怖仇哆,靈堂內(nèi)的尸體忽然破棺而出沦辙,到底是詐尸還是另有隱情,我是刑警寧澤讹剔,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布油讯,位于F島的核電站,受9級特大地震影響延欠,放射性物質(zhì)發(fā)生泄漏陌兑。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一由捎、第九天 我趴在偏房一處隱蔽的房頂上張望兔综。 院中可真熱鬧,春花似錦、人聲如沸软驰。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽碌宴。三九已至杀狡,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間贰镣,已是汗流浹背呜象。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留碑隆,地道東北人恭陡。 一個月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓,卻偏偏與公主長得像上煤,于是被迫代替她去往敵國和親休玩。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容