iOS PickerView选择视图

 

原文demo:

@interface ViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    
    UIPickerView * pickerView;
    
    NSMutableArray * listPicker;
    NSMutableArray * listPicker2;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.

    [self CreatUI];

    UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    back.frame = CGRectMake(20, 60, 40, 40);
    back.backgroundColor = [UIColor greenColor];
    [back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:back];
    
}
-(void)backk{
    
    [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil];
    
}

-(void)CreatUI{
    
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(50, 100, 300, 400)];
    
    //显示选中框
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate =self;
    pickerView.dataSource  = self;
    
    [self.view addSubview:pickerView];
    
    
    listPicker = [NSMutableArray array];
    listPicker2 = [NSMutableArray array];
    
    
    //dataSoure
    for (int i =0; i<100; i++) {
        
        
        [listPicker addObject:[NSString stringWithFormat:@"%d",i]];
        [listPicker2 addObject:[NSString stringWithFormat:@"%02d",i]];
        
    }
    
}

/////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 2;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return listPicker.count;//每列的row个数
    
    
}

//列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    if (component==0) {
        return 80;
    }else{
        return 80;
    }
    
}
//
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
  
    if (component==0) {
        return 100   ;
    }else{
        
        return 100;
    }
    
    
}
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    
}


//返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component ==0) {
        return [NSString stringWithFormat:@"%@ 列",[listPicker objectAtIndex:row]];
    }else{
        
        return [NSString stringWithFormat:@"%@ 个",[listPicker objectAtIndex:row]];
    }
    

    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
一、两列 独立旋转

 

 

@interface LinkageViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>

{
    
    UIPickerView * pickerViewv;
    
    NSArray * listPicker;//sheng
    NSArray * listPicker2;//shi
    
}
@property(nonatomic,strong) UILabel * label;

@end

@implementation LinkageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self CreatUI];
    
    UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    back.frame = CGRectMake(20, 60, 40, 40);
    back.backgroundColor = [UIColor greenColor];
    [back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:back];
    
}
-(void)backk{
    
    [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil];
    
}


-(void)CreatUI{
    
    pickerViewv = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 400)];
    
    //显示选中框
    pickerViewv.showsSelectionIndicator = YES;
    pickerViewv.delegate =self;
    pickerViewv.dataSource  = self;
    
    [self.view addSubview:pickerViewv];
    
    
    listPicker = [NSMutableArray array];
    listPicker2 = [NSMutableArray array];
    
    
    
    //读取数据
    NSString * path = [[NSBundle mainBundle]pathForResource:@"area" ofType:@"plist"];
    listPicker = [NSArray arrayWithContentsOfFile:path];
    listPicker2 = [NSArray arrayWithArray:listPicker[0][@"Cities"]];
    
    
    
    //Label
    //label的布局约束
    self.label = [[UILabel alloc]initWithFrame:CGRectZero];
    self.label.translatesAutoresizingMaskIntoConstraints = NO;
    self.label.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.label.textColor = [UIColor greenColor];
    self.label.font = [UIFont systemFontOfSize:30];
    [self.label setTextAlignment:NSTextAlignmentCenter];
    [self.view addSubview:self.label];
    NSArray * labelTop = [NSLayoutConstraint constraintsWithVisualFormat:@"V:[pickerViewv]-30-[_label(50)]" options:0 metrics:nil views:NSDictionaryOfVariableBindings(pickerViewv,_label)];
    NSArray * labelH = [NSLayoutConstraint constraintsWithVisualFormat:@"H:|-20-[_label]-20-|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_label)];
    [self.view addConstraints:labelTop];
    [self.view addConstraints:labelH];
}

/////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 2;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    NSInteger rows =0;
    
    switch (component) {
        case 0:
            rows = listPicker.count;
            break;
        case 1:
            rows = listPicker2.count;
            
        default:
            break;
    }
    
    return rows;
}

//列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    if (component==0) {
        return 150;
    }else{
        return 150;
    }
    
}
//
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    if (component==0) {
        return 60   ;
    }else{
        
        return 60;
    }
    
    
}

//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component==0) {
        
        listPicker2 = listPicker[row][@"Cities"];
        [pickerViewv reloadComponent:1];//刷新第几个
    }else{
        
        self.label.text = [NSString stringWithFormat:@"%@%@",listPicker[[pickerViewv selectedRowInComponent:0 ]][@"State"],listPicker2[[pickerViewv selectedRowInComponent:1]][@"city"]];
        
    }
    
    
    
}


//返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component ==0) {
        return listPicker[row][@"State"];
    }else{
        
        return listPicker2[row][@"city"];
    }
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
二、第二列随着第一列刷新

 

 

@interface CustomViewController ()<UIPickerViewDelegate,UIPickerViewDataSource>
{
    
    UIPickerView * pickerView;
    
    NSMutableArray * listPicker;
    NSMutableArray * listPicker2;
}


@end

@implementation CustomViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self CreatUI];
    UIButton *back = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    
    back.frame = CGRectMake(20, 60, 40, 40);
    back.backgroundColor = [UIColor greenColor];
    [back addTarget:self action:@selector(backk) forControlEvents:UIControlEventTouchUpInside];
    
    [self.view addSubview:back];
    
}
-(void)backk{
    
    [self presentViewController:[[RootViewController alloc]init] animated:YES completion:nil];
    
}


-(void)CreatUI{
    
    pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 50, self.view.frame.size.width, self.view.frame.size.height-50)];
    
    //显示选中框
    pickerView.showsSelectionIndicator = YES;
    pickerView.delegate =self;
    pickerView.dataSource  = self;
    
    [self.view addSubview:pickerView];
    
    
    listPicker = [NSMutableArray array];
    listPicker2 = [NSMutableArray array];
    
    
    //dataSoure
    for (int i =0; i<100; i++) {
        
        
        [listPicker addObject:[NSString stringWithFormat:@"%d",i]];
        [listPicker2 addObject:[NSString stringWithFormat:@"%02d",i]];
        
    }
    
}

/////相关代理
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return 2;//列数
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    
    return listPicker.count;//每列的row个数
    
    
}

//列宽
-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    
    if (component==0) {
        return 80;
    }else{
        return 40;
    }
    
}
//
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    if (component==0) {
        return 80   ;
    }else{
        
        return 40;
    }
    
    
}
//选中行
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    
    if (component==0) {
        //选中第一列。一般刷新第二列
        
        // [pickerViewv reloadComponent:1];//刷新第几个
      
    }else{
        //选中第二列。一般输出
        
        
        
           }
    
    
    
}
#pragma mark 给pickerview设置字体大小和颜色等
-(UIView*)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    
    //可以自定义label
    UILabel * pickerlabel = (UILabel*)view;
    if (!pickerlabel) {
        
        pickerlabel = [[UILabel alloc] init];
        pickerlabel.adjustsFontSizeToFitWidth = YES;
        pickerlabel.textAlignment = NSTextAlignmentCenter;
        [pickerlabel setBackgroundColor:[UIColor lightGrayColor]];
        [pickerlabel setFont:[UIFont systemFontOfSize:23]];
        
    }
    
    pickerlabel.text = [self pickerView:pickerView titleForRow:row forComponent:component];
    
    return pickerlabel;
    
}

//返回当前行的内容 此处将是数组中数值添加到滚动的那个显示兰 批量操作显示的内容
-(NSString*)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    if (component ==0) {
        return [NSString stringWithFormat:@"%@ 列",[listPicker objectAtIndex:row]];
    }else{
        
        return [NSString stringWithFormat:@"%@ 个",[listPicker objectAtIndex:row]];
    }
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
三、自定义显示View

 

posted @ 2017-06-19 15:57  徐家汇  阅读(174)  评论(0编辑  收藏  举报