首先半抱、為了文章有個好看的封面胯杭、特附今早狂風(fēng)下拍的一張自認(rèn)為還挺好看的圖片驯杜、哈哈
哈哈.jpg
應(yīng)公司項目需求要實現(xiàn)一個簡單的下拉選擇的功能,故今天順便寫一下文章做个。
先上圖吧鸽心、請再次原諒和忽略本寶寶粗糙的審美、圖丑湊合著看居暖、意思到了就行哈~
其實思路很簡單顽频、我們的UI控件中有一個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;
// 添加點擊手勢
UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseViewShowOrHide)];
[self.textf addGestureRecognizer:tapGes];
}
// 點擊手勢的執(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ù)自己的需求去寫一個比我這個漂亮不知道多少次方倍的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 點擊事件
// 1、將cell中的相關(guān)數(shù)據(jù)賦值給textf
// 2略贮、同時隱藏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];
}
到此為止甚疟,一個“簡潔大方”的下拉選擇框就寫成了仗岖。
希望我的文章對大家有所幫助±姥康撒米噠~~~
千山萬水總是情
關(guān)注一下行不行 (*^_^*)