簡單的下拉選擇框

首先、為了文章有個(gè)好看的封面、特附今早狂風(fēng)下拍的一張自認(rèn)為還挺好看的圖片、哈哈


哈哈.jpg

應(yīng)公司項(xiàng)目需求要實(shí)現(xiàn)一個(gè)簡單的下拉選擇的功能,故今天順便寫一下文章。
先上圖吧、請(qǐng)?jiān)俅卧徍秃雎员緦殞毚植诘膶徝?、圖丑湊合著看、意思到了就行哈~


其實(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)擊手勢(shì)
    UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chooseViewShowOrHide)];
    [self.textf addGestureRecognizer:tapGes];
    
}

// 點(diǎn)擊手勢(shì)的執(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è)“簡潔大方”的下拉選擇框就寫成了。
希望我的文章對(duì)大家有所幫助??等雒讎}~~~

千山萬水總是情
關(guān)注一下行不行  (*^_^*)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 15,043評(píng)論 4 61
  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點(diǎn)贊按鈕進(jìn)度條TabLayout圖標(biāo)下拉刷新...
    皇小弟閱讀 47,133評(píng)論 22 665
  • 浴缸不大,水位漲到邊沿了,蜷起腿時(shí)一雙膝蓋像浮動(dòng)的裸色冰山,聳立著。塑料泛黃的花灑掛在高處,大粒的水珠砸到藍(lán)色水面...
    天吶天吶那天會(huì)來噠__閱讀 335評(píng)論 1 1
  • 每天都感覺自己很忙,其實(shí)是碌碌而為!在自己忙碌的生活中,若再加入一些事情,其實(shí)你還是那樣忙! 從上個(gè)周日到這...
    佳玉兒閱讀 426評(píng)論 0 0
  • 霓虹閃爍的夜妖冶迷人,白小柏坐在副駕駛的位置插著耳機(jī)閉上了眼睛。車?yán)锪魈手鴾責(zé)釙崦恋臍庀ⅲ疽杏X到自己的臉微微發(fā)...
    一個(gè)藝閱讀 371評(píng)論 3 1

友情鏈接更多精彩內(nèi)容