簡單的下拉選擇框

首先、為了文章有個好看的封面、特附今早狂風下拍的一張自認為還挺好看的圖片、哈哈


哈哈.jpg

應公司項目需求要實現一個簡單的下拉選擇的功能,故今天順便寫一下文章。
先上圖吧、請再次原諒和忽略本寶寶粗糙的審美、圖丑湊合著看、意思到了就行哈~


其實思路很簡單、我們的UI控件中有一個UITextfield、他是可以設置內部左右自定義視圖的、另外我們下拉的視圖就用tableview去寫。

哎、還是直接上代碼吧、辣么詳細的注釋已經不需要我再啰嗦什么了。

#import "ViewController.h"

@interface ViewController ()<UITableViewDelegate,UITableViewDataSource>

// 展示textf的rightView圖標的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];
    
    // 設置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];
    
}

// 點擊手勢的執行事件
// 改變imageView中的圖標
- (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];
    }
}

當然,這里我只是為了說明效果、沒有去自定義cell、大家可以根據自己的需求去寫一個比我這個漂亮不知道多少次方倍的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中的相關數據賦值給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];
}

到此為止,一個“簡潔大方”的下拉選擇框就寫成了。
希望我的文章對大家有所幫助。康撒米噠~~~

千山萬水總是情
關注一下行不行  (*^_^*)
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • 發現 關注 消息 iOS 第三方庫、插件、知名博客總結 作者大灰狼的小綿羊哥哥關注 2017.06.26 09:4...
    肇東周閱讀 12,252評論 4 61
  • 內容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 46,903評論 22 665
  • 浴缸不大,水位漲到邊沿了,蜷起腿時一雙膝蓋像浮動的裸色冰山,聳立著。塑料泛黃的花灑掛在高處,大粒的水珠砸到藍色水面...
    天吶天吶那天會來噠__閱讀 282評論 1 1
  • 每天都感覺自己很忙,其實是碌碌而為!在自己忙碌的生活中,若再加入一些事情,其實你還是那樣忙! 從上個周日到這...
    佳玉兒閱讀 375評論 0 0
  • 霓虹閃爍的夜妖冶迷人,白小柏坐在副駕駛的位置插著耳機閉上了眼睛。車里流淌著溫熱曖昧的氣息,簿耀感覺到自己的臉微微發...
    一個藝閱讀 343評論 3 1