直接粘贴代码
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong, nonatomic) IBOutlet UIPickerView *pickerView;
@property(nonatomic,strong)NSMutableArray *hArr;
@property(nonatomic,strong)NSMutableArray *mArr;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"%@",self.hArr);
NSLog(@"===%@",self.mArr);
_pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(25, 35, self.view.bounds.size.width-50, 80)];
_pickerView.backgroundColor = [UIColor clearColor];
_pickerView.delegate = self;
_pickerView.dataSource = self;
_pickerView.delegate = self;
_pickerView.dataSource = self;
[self.view addSubview:self.pickerView];
// Do any additional setup after loading the view.
}
#pragma mark pickerviewDelegate
//返回列数
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return2;
}
//返回每列行数
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if(component==0) {
return 24;
}else{
return 60;
}
}
// 返回每一行的标题
-(NSString*)pickerView:(UIPickerView*)pickerViewtitleForRow:(NSInteger)rowforComponent:(NSInteger)component
{
if(component ==0)
{
returnself.hArr[row];
}else
{
returnself.mArr[row];
}
}
//改变选中那行的字体和颜色
- (UIView*)pickerView:(UIPickerView*)pickerViewviewForRow:(NSInteger)rowforComponent:(NSInteger)componentreusingView:(UIView*)view{
if(!view){
view = [[UIViewalloc]init];
}
UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (self.view.bounds.size.width-50)/3, 20)];
if(component ==2) {
NSIntegerselecrDay = [_pickerViewselectedRowInComponent:component];
if(selecrDay == row) {
text.textColor = [UIColor colorWithRed:13/255.f green:152/255.f blue:215/255.f alpha:1];
}
}
text.textAlignment = NSTextAlignmentCenter;
if(component==0) {
text.text= [_hArrobjectAtIndex:row];
}
if(component==1) {
text.text= [_mArrobjectAtIndex:row];
}
[viewaddSubview:text];
returnview;
}
///被选择的行
-(void)pickerView:(UIPickerView*)pickerViewdidSelectRow:(NSInteger)rowinComponent:(NSInteger)component{
UIView* view = [pickerViewviewForRow:rowforComponent:component];
UILabel* label = view.subviews.firstObject;
label.textColor =[UIColor redColor];
label.font = [UIFont systemFontOfSize:30.0];
}
#pragma mark- 懒加载
-(NSMutableArray *)mArr{
if(!_mArr){
_mArr= [NSMutableArrayarray];
for(inti =0; i<60; i++) {
NSString*Str= [NSStringstringWithFormat:@"%02d",i];
[_mArraddObject:Str];
}
}
return_mArr;
}
-(NSMutableArray *)hArr{
if(!_hArr){
_hArr= [NSMutableArrayarray];
for(inti =0; i<24; i++) {
NSString*Str= [NSStringstringWithFormat:@"%02d",i];
[_hArraddObject:Str];
}
}
return_hArr;
}
@end
主要代码
//改变选中那行的字体和颜色
- (UIView*)pickerView:(UIPickerView*)pickerViewviewForRow:(NSInteger)rowforComponent:(NSInteger)componentreusingView:(UIView*)view{
if(!view){
view = [[UIViewalloc]init];
}
UILabel *text = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, (self.view.bounds.size.width-50)/3, 20)];
if(component ==2) {
NSIntegerselecrDay = [_pickerViewselectedRowInComponent:component];
if(selecrDay == row) {
text.textColor = [UIColor colorWithRed:13/255.f green:152/255.f blue:215/255.f alpha:1];
}
}
text.textAlignment = NSTextAlignmentCenter;
if(component==0) {
text.text= [_hArrobjectAtIndex:row];
}
if(component==1) {
text.text= [_mArrobjectAtIndex:row];
}
[viewaddSubview:text];
returnview;
}
///被选择的行
-(void)pickerView:(UIPickerView*)pickerViewdidSelectRow:(NSInteger)rowinComponent:(NSInteger)component{
UIView* view = [pickerViewviewForRow:rowforComponent:component];
UILabel* label = view.subviews.firstObject;
label.textColor =[UIColor redColor];
label.font = [UIFont systemFontOfSize:30.0];
}
网友评论