首先、為了文章有個(gè)好看的封面、特附今早狂風(fēng)下拍的一張自認(rèn)為還挺好看的圖片、哈哈
哈哈.jpg
應(yīng)公司項(xiàng)目需求要實(shí)現(xiàn)一個(gè)簡單的下拉選擇的功能,故今天順便寫一下文章。
先上圖吧、請?jiān)俅卧徍秃雎员緦殞毚植诘膶徝馈D丑湊合著看、意思到了就行哈~
其實(shí)思路很簡單、我們的UI控件中有一個(gè)UITextfield、他是可以設(shè)置內(nèi)部左右自定義視圖的、另外我們下拉的視圖就用tableview去寫。
哎、還是直接上代碼吧、辣么詳細(xì)的注釋已經(jīng)不需要我再啰嗦什么了。
#import "ViewController.h"
@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>
// 展示textf的rightView圖標(biāo)的imageView
@property (nonatomic,strong) UIImageView *imageV;
@property (nonatomic ,strong) UITextField *textf;
@property (nonatomic,strong) UITableView *popChooseView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
[self textfield];
[self setupPopChooseView];
}
- (void)textfield
{
self.textf = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 120, 40)];
self.textf.borderStyle = UITextBorderStyleRoundedRect;
self.textf.text = @"英語";
self.textf.tag = 50;
[self.view addSubview:self.textf];
// 設(shè)置textf的rightView
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
view.backgroundColor = [UIColor clearColor];
self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(5, 5, 20, 20)];
self.imageV.image = [UIImage imageNamed:@"Down"];
self.imageV.tag = 100;
self.imageV.contentMode = UIViewContentModeCenter;
[view addSubview:self.imageV];
self.textf.rightView = view;
self.textf.rightViewMode = UITextFieldViewModeAlways;
// 添加點(diǎn)擊手勢
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseViewShowOrHide)];
[self.textf addGestureRecognizer:tapGes];
}
// 點(diǎn)擊手勢的執(zhí)行事件
// 改變imageView中的圖標(biāo)
- (void)chooseViewShowOrHide
{
if (self.imageV.tag == 100) {
self.imageV.tag = 101;
self.imageV.image = [UIImage imageNamed:@"up"];
[self.view addSubview:self.popChooseView];
}else
{
self.imageV.tag = 100;
self.imageV.image = [UIImage imageNamed:@"Down"];
[self.popChooseView removeFromSuperview];
}
}
當(dāng)然,這里我只是為了說明效果、沒有去自定義cell、大家可以根據(jù)自己的需求去寫一個(gè)比我這個(gè)漂亮不知道多少次方倍的cell
- (void)setupPopChooseView
{
UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(20, 139, 120, 150) style:UITableViewStylePlain];
tableView.delegate = self;
tableView.dataSource = self;
tableView.rowHeight = 30;
self.popChooseView = tableView;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellID = @"popCell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
}
cell.backgroundColor = [UIColor lightGrayColor];
cell.textLabel.text = [NSString stringWithFormat:@"英語-%d",(int)indexPath.row];
return cell;
}
// tableViewCell 點(diǎn)擊事件
// 1、將cell中的相關(guān)數(shù)據(jù)賦值給textf
// 2、同時(shí)隱藏popChooseView
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
[tableView deselectRowAtIndexPath:indexPath animated:YES];
UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
self.textf.text = cell.textLabel.text;
[self chooseViewShowOrHide];
}
到此為止,一個(gè)“簡潔大方”的下拉選擇框就寫成了。
希望我的文章對大家有所幫助。康撒米噠~~~
千山萬水總是情
關(guān)注一下行不行 (*^_^*)