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); } }
效果:
时间: 2024-10-26 14:18:36