自己封装UIPickerView加一个时间范围

1.添加代理:

@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

2.添加存储数组和视图:

//视图

@property(nonatomic,strong)UIPickerView *pickerView;

//日期
@property(nonatomic,strong)NSMutableArray *dayDateArray;

//消失
@property(nonatomic,strong)NSMutableArray *hoursDateArray;


3创建视图:

- (void)creatBtn{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(30, 30, 100, 30)];
    [btn setTitle:@"点击" forState:UIControlStateNormal];
    [btn setTintColor:[UIColor blackColor]];
    [btn setBackgroundColor:[UIColor redColor]];
    [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
}

- (void)btnAction:(UIButton *)sender{
    [self creatPicker];
}


- (void)creatPicker{
    
    NSString *strValue ;
    for (int i = 0; i < 24; i++) {
        if (i + 1 < 10) {
            strValue  = [NSString stringWithFormat:@"0%d:00 - 0%d:00",i ,i + 1];
        }else if(i + 1 == 10){
            strValue = [NSString stringWithFormat:@"0%d:00 - %d:00",i ,i + 1];
        }else if(i + 1>10 && i + 1 < 24){
             strValue = [NSString stringWithFormat:@"%d:00 - %d:00",i ,i + 1];
        }else if (i+1 == 24) {
            strValue = [NSString stringWithFormat:@"%d:00 - 00:00",i];
        }
        [self.hoursDateArray addObject:strValue];
    }
    
    NSInteger dateMonth = 0;
    NSInteger dateDay = 0;
    
    for (int i = 0; i< 31; i++) {
        NSDate *date = [NSDate dateWithTimeIntervalSinceNow:i*(24*60*60)];
        
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *comps = [calendar components:(NSCalendarUnitMonth |NSCalendarUnitDay ) fromDate:date];
        dateMonth = [comps month];
        dateDay = [comps day];
        NSString *dateVaule ;
        if (i == 0) {
            dateVaule = @"今天";
        }else{
            dateVaule = [NSString stringWithFormat:@"%ld月%ld日",dateMonth,dateDay];
        }
        [self.dayDateArray addObject:dateVaule];
    }
    
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
    self.pickerView.autoresizingMask = UIViewAutoresizingFlexibleWidth;
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    self.pickerView.showsSelectionIndicator = YES;
    [self.view addSubview:self.pickerView];
}

//重写协议方法:

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

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.dayDateArray.count;
    }else{
        return self.hoursDateArray.count;
    }
    
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *myView = nil;
    if (component == 0) {
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 100, 30)] ;
        
        myView.textAlignment = NSTextAlignmentCenter;
        
        myView.text = [self.dayDateArray objectAtIndex:row];
        
        myView.font = [UIFont systemFontOfSize:17];         //用label来设置字体大小
        
        myView.backgroundColor = [UIColor clearColor];
    }else{
        myView = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 0.0, 150, 30)];
        
        myView.text = [self.hoursDateArray objectAtIndex:row];
        
        myView.textAlignment = NSTextAlignmentCenter;
        
        myView.font = [UIFont systemFontOfSize:17];
        
        myView.backgroundColor = [UIColor clearColor];
    }
    return myView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView widthHeightForComponent:(NSInteger)component{
    
    if (component == 0) {
        return 100;
    }else{
        return 150;
    }
}

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

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if((self.dayDateArray.count-1>=row && component == 0)&&(self.hoursDateArray.count-1>= row && component == 1)){
    NSString *strVaule = [NSString stringWithFormat:@"%@  %@",[self.dayDateArray objectAtIndex:row],[self.hoursDateArray objectAtIndex:row-2]];
    NSLog(@"%@",strVaule);
    }
}

效果:


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值