iOS PickerView自定义使用方法总结

准备工作:

新建pch文件,提前写入需要的宽高宏定义:

//屏幕适配尺寸,适应于整个屏幕

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width

#define SCREEN_HEIGHT [UIScreen mainScreen].bounds.size.height

#define Width [UIScreen mainScreen].bounds.size.width/320.0

#define Height [UIScreen mainScreen].bounds.size.height/480.0

View创建和初始化

  一、新建类,继承UIView;遵循pickerView代理

<UIPickerViewDataSource,UIPickerViewDelegate>

需要有页面view、pickerview、取消按钮、确定按钮、picker标题label、存放pickerview和取消、确定、标题label的View;

    1、定义控件

{
//选择每列需要返回的内容
    NSString *oneStr;
    NSString *twoStr;
    NSString *threeStr;
}
//确定和取消按钮
@property(nonatomic,strong)UIButton *cancelBtn;
@property(nonatomic,strong)UIButton *sureBtn;
//标题
@property(nonatomic,strong)UILabel *titlelabel;

//显示多少行,最多显示3行
@property(nonatomic,assign)int rowcount;

@property(nonatomic,strong)UIPickerView *pickView;
//背景视图
@property(nonatomic,strong)UIView *backView;
//每列需要显示的数据
//第一列
@property(nonatomic,strong)NSMutableArray *onePick;
//第二列
@property(nonatomic,strong)NSMutableArray *twoPick;
//第三列
@property(nonatomic,strong)NSMutableArray *threePick;

       新建代理

@class PickerViewExtension;
@protocol pickViewselectDelegate <NSObject>

-(void)pickViewdelegateWith:(NSString *)oneStr AndStr:(NSString *)twoStr WithStr:(NSString *)threeStr;

@end


@property(nonatomic,assign)id<pickViewselectDelegate>delegate;

2、初始化View

-(instancetype)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    if (self) {
        
        //页面透明背景相当于蒙版
        _backView =[[UIView alloc]initWithFrame:CGRectMake(0,  0,SCREEN_WIDTH, SCREEN_HEIGHT)];
        _backView.backgroundColor =[UIColor colorWithRed:0 green:0 blue:0 alpha:0.3];
        [self addSubview:_backView];
        
        //存放按钮、picker的白色背景
        UIView *view =[[UIView alloc]initWithFrame:CGRectMake(0,  SCREEN_HEIGHT - 180*Height,SCREEN_WIDTH, 30*Height)];
        view.backgroundColor =[UIColor colorWithRed:57/255.0 green:176/255.0 blue:37/255.0 alpha:1];
        [_backView addSubview:view];
        //picker的标题label
        _titlelabel =[[UILabel alloc]initWithFrame:CGRectMake(100*Width, 5*Height, 130*Width, 20*Height)];
        _titlelabel.textAlignment =NSTextAlignmentCenter;
        _titlelabel.textColor =[UIColor whiteColor];
        [view addSubview:_titlelabel];
        
        //picker创建
        _pickView =[[UIPickerView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - 150*Height, SCREEN_WIDTH, 150*Height)];
        _pickView.delegate = self;
        _pickView.dataSource = self;
        _pickView.backgroundColor =[UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1.0];
        _pickView.showsSelectionIndicator = YES;
        [self addSubview:_pickView];

        //取消、确定按钮创建
        _cancelBtn =[[UIButton alloc]initWithFrame:CGRectMake(10*Width, 0*Height, 50*Width, 30*Height)];
        [_cancelBtn addTarget:self action:@selector(cancelpickview) forControlEvents:UIControlEventTouchUpInside];
        
        [_cancelBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
        [view addSubview:_cancelBtn];
        
        _sureBtn = [[UIButton alloc]initWithFrame:CGRectMake(260*Width, 0*Height, 50*Width, 30*Height)];
        [_sureBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_sureBtn addTarget:self action:@selector(sureselectBtn) forControlEvents:UIControlEventTouchUpInside];
        [_sureBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [view addSubview:_sureBtn];
        
        [_pickView reloadAllComponents];
        
    }
    return self;
}

    确定和取消按钮方法

//选择方法
-(void)sureselectBtn
{
    if ([self.delegate respondsToSelector:@selector(pickViewdelegateWith:AndStr: WithStr:)]) {
        [self.delegate pickViewdelegateWith:oneStr AndStr:twoStr WithStr:threeStr];
    }
}

//取消方法
-(void)cancelpickview
{
    [self removeFromSuperview];
}

2、实现pickerView的代理方法

     1》显示picker多少列

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return _rowcount;
}

    2》显示每列多少行

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0) {
        return _onePick.count;
    }
    if (component == 1) {
        return _twoPick.count;
    }
    return _threePick.count;
}

    3》每行的高度

-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 25*Height;
}

    4》每行显示的内容

-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0) {
        oneStr = [NSString stringWithFormat:@"%@",_onePick[row]];
        return oneStr;
    }
    if (component == 1) {
        twoStr = _twoPick[row];
        return _twoPick[row];
    }
    
    threeStr = _threePick[row];
    return _threePick[row];
    
}

    5》选择后需要执行的方法

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 2) {
        threeStr = _threePick[row];
        NSInteger inter =[self isAllDay:oneStr.intValue andMonth:twoStr.intValue];
        _threePick =[NSMutableArray array];
        for (int i =0; i<inter; i++) {
            NSString *str =[NSString stringWithFormat:@"%d日",i+1];
            [_threePick addObject:str];
        }
        [_pickView reloadComponent:2];
        return;
    }
    if (component == 1) {
        twoStr  = _twoPick[row];
        if (_rowcount == 3) {
            threeStr = _threePick[row];
            NSInteger inter =[self isAllDay:oneStr.intValue andMonth:twoStr.intValue];
            _threePick =[NSMutableArray array];
            for (int i =0; i<inter; i++) {
                //每个月有多少天
                NSString *str =[NSString stringWithFormat:@"%d日",i+1];
                [_threePick addObject:str];
            }
            [_pickView reloadComponent:2];
        }
        return;
    }
    oneStr = _onePick[row];
    
}

//判断每月有多少天
-(NSInteger)isAllDay:(NSInteger)year andMonth:(NSInteger)month
{
    int day=0;
    switch(month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            day=31;
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            day=30;
            break;
        case 2:
        {
            if(((year%4==0)&&(year%100!=0))||(year%400==0))
            {
                day=29;
                break;
            }
            else
            {
                day=28;
                break;
            }
        }
        default:
            break;
    }
    return day;
}

二、总结

自定义控件外观可以随意更改、功能更加全面,使用比较方便;通过行数可以随意切换,时间、地址、分类等等。

转载于:https://my.oschina.net/u/2619390/blog/837764

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值