IOS 自定義下拉選擇控件

.h文件代碼數組

#import <UIKit/UIKit.h>

@interface DropDown : UIView <UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate> {
    
    UITableView *tv;//下拉列表
    
    NSArray *tableArray;//下拉列表數據
    
    UITextField *textField;//文本輸入框
    
    BOOL showList;//是否彈出下拉列表
    
    CGFloat tabheight;//table下拉列表的高度
    
    CGFloat frameHeight;//frame的高度
    
}


@property (nonatomic,retain) UITableView *tv;

@property (nonatomic,retain) NSArray *tableArray;

@property (nonatomic,retain) UITextField *textField;


@end

.m文件中代碼atom

#import "DropDown.h"

@implementation DropDown

@synthesize tv,tableArray,textField;

-(id)initWithFrame:(CGRect)frame

{
    
    if (frame.size.height<200) {
        frameHeight = 200;
    }else{
        frameHeight = frame.size.height;
    }
//    tabheight = frameHeight-30;
    tabheight = 70;
    frame.size.height = 30.0f;
    self=[super initWithFrame:frame];
    if(self){
        showList = NO; //默認不顯示下拉框
        tv = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 0)];
        tv.delegate = self;
        tv.dataSource = self;
        tv.backgroundColor = [UIColor grayColor];
        tv.separatorColor = [UIColor lightGrayColor];
        tv.hidden = YES;
        [self addSubview:tv];
        
        textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 30)];
        textField.borderStyle=UITextBorderStyleRoundedRect;//設置文本框的邊框風格
        [textField addTarget:self action:@selector(dropdown) forControlEvents:UIControlEventAllTouchEvents];
        [self addSubview:textField];
        textField.delegate = self;//這一句要加入代理
    }
    return self;
}
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    return NO;
}
-(void)dropdown{
    
    [textField resignFirstResponder];
    if (showList) {//若是下拉框已顯示,什麼都不作
        return;
    }else {//若是下拉框還沒有顯示,則進行顯示
        CGRect sf = self.frame;
        sf.size.height = frameHeight;
        //把dropdownList放到前面,防止下拉框被別的控件遮住
        [self.superview bringSubviewToFront:self];
        tv.hidden = NO;
        showList = YES;//顯示下拉框

        CGRect frame = tv.frame;
        frame.size.height = 0;
        tv.frame = frame;
        frame.size.height = tabheight;
        [UIView beginAnimations:@"ResizeForKeyBoard" context:nil];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        self.frame = sf;
        tv.frame = frame;
        [UIView commitAnimations];
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [tableArray count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }
    cell.textLabel.text = [tableArray objectAtIndex:[indexPath row]];
    cell.textLabel.font = [UIFont systemFontOfSize:16.0f];
    cell.accessoryType = UITableViewCellAccessoryNone;
    cell.selectionStyle = UITableViewCellSelectionStyleGray;

    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 35;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    textField.text = [tableArray objectAtIndex:[indexPath row]];
    showList = NO;
    tv.hidden = YES;
    
    CGRect sf = self.frame;
    sf.size.height = 30;
    self.frame = sf;
    
    CGRect frame = tv.frame;
    frame.size.height = 0;
    tv.frame = frame;
    
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


@end

調用代碼:(dataSource自定義數組)代理

DropDown *dropDown = [[DropDown alloc] initWithFrame:CGRectMake(100, 40, 150, 30)];
    dropDown.textField.placeholder = @"請選擇購買時長";
    dropDown.tableArray = dataSource;
    [self.view addSubview:dropDown];

關注新平臺:http://www.toutiao.com/m51416718261/code

關注訂閱號:從小就壞pdo

相關文章
相關標籤/搜索