ios Objective-c UITextField 实现下拉选

调用方法initDropDownMenu就可以让这个UITextField变成点击选择文本的了,传的数组就是让选择的内容

///导入UITextField+drop_down_menu.h
#import "UITextField+drop_down_menu.h"



UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(21, 150, 100, 15)];

[textField initDropDownMenu:@[@"0:00",@"0:30",@"1:00",@"1:30",@"2:00",@"2:30",@"3:00",@"3:30",@"4:00",@"4:30",@"5:00",@"5:30",@"6:00",@"6:30",@"7:00",@"7:30",@"8:00",@"8:30",@"9:00",@"9:30",@"10:00",@"10:30",@"11:00",@"11:30",@"12:00",@"12:30",@"13:00",@"13:30",@"14:00",@"14:30",@"15:00",@"15:30",@"16:00",@"16:30",@"17:00",@"17:30",@"18:00",@"18:30",@"19:00",@"19:30",@"20:00",@"20:30",@"21:00",@"21:30",@"22:00",@"22:30",@"23:00",@"23:30"]];

[self.view addSubView:textField];


UITextField+drop_down_menu文件实现

#import 
@interface UITextField (drop_down_menu)<UITableViewDelegate,UITableViewDataSource>


- (void) initDropDownMenu:(NSArray *) list;


@end


#import "UITextField+drop_down_menu.h"

@implementation UITextField (drop_down_menu)

UITableView  *tableView;

NSArray * listStr;

- (void) initDropDownMenu:(NSArray *) list {
    
    NSInteger height = 0;
    
    if (list.count<10) {
        
        height = list.count * 24;
        
    }else{
        
        height = 240;
        
    }
    
     
    
    tableView = [[UITableView alloc] initWithFrame:CGRectMake(self.frame.origin.x , self.frame.origin.y + self.frame.size.height, self.frame.size.width, height)];
        
       listStr = list;
        
        tableView.delegate = self;
        
        tableView.dataSource = self;
        
        tableView.backgroundColor = [UIColor whiteColor];
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

        tableView.alpha = 1.0;
        
        [self.superview addSubview:tableView];
        
        [tableView setHidden:true];
    
    UIButton *didBtn = [[UIButton alloc] initWithFrame:self.frame];
    
    [didBtn addTarget:self action:@selector(beginSelect) forControlEvents:UIControlEventTouchUpInside];
    
    [self.superview addSubview:didBtn];
    
    
}

- (void) beginSelect{
    
    [tableView setHidden:false];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 24.0;
    
}
//表格行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return listStr.count;
}


//初始化cell
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 24.0)];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 24.0)];
    //indexPath.row表示这是第几行,从0开始
    NSString *showStr = listStr[indexPath.row];
    
    //10-00
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:showStr];
    [attributedString addAttribute:NSFontAttributeName value:[UIFont fontWithName:@"PingFangSC-Regular" size:14.0f] range:NSMakeRange(0, attributedString.length)];
    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:134.0f/255.0f green:134.0f/255.0f blue:134.0f/255.0f alpha:1.0f] range:NSMakeRange(0, attributedString.length)];
    
    [label setAttributedText:attributedString];
    [label setTextAlignment:NSTextAlignmentCenter];
    [cell.contentView addSubview:label];
    
    //在初始化cell时加上这一句,可以让点击特效消失.不影响功能
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
 
    NSString *showStr = listStr[indexPath.row];
    
    [tableView setHidden:true];
    
    [self setText:showStr];
    
}

@end



你可能感兴趣的:(IOS,-,OC,专栏)