UIKit 框架之UIPickerView

//

//  ViewController.m

//  UIPickerView

//

//  Created by City--Online on 15/5/18.

//  Copyright (c) 2015年 XQB. All rights reserved.

//



#import "ViewController.h"



@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>

@property(nonatomic,strong) UIPickerView *pickerView;

@property(nonatomic,strong) NSMutableArray *provinceArr;

@property(nonatomic,strong) NSMutableArray *cityArr; //省份对应下的城市及显示的城市

@property(nonatomic,strong) NSDictionary *dicData;

@end



@implementation ViewController



- (void)viewDidLoad {

    [super viewDidLoad];

    _pickerView=[[UIPickerView alloc]init];

    CGSize size=[UIScreen mainScreen].bounds.size;

    _pickerView.frame=CGRectMake(10, 10, size.width, 100);

    _pickerView.showsSelectionIndicator=YES;

    _pickerView.dataSource=self;

    _pickerView.delegate=self;

    [self.view addSubview:_pickerView];

    

    _provinceArr=[[NSMutableArray alloc]initWithObjects:@"河南",@"广东", nil];

    NSArray *arr1=[[NSArray alloc]initWithObjects:@"----",@"驻马店",@"周口",nil];

    NSArray *arr2=[[NSArray alloc]initWithObjects:@"----",@"深圳",@"广州",@"东莞", nil];

    _dicData=[[NSDictionary alloc]initWithObjects:[[NSArray alloc] initWithObjects:arr1,arr2, nil] forKeys:_provinceArr];

    

    //找到第一个轮子选择行的索引

    NSInteger selectProvinceIndex=[_pickerView selectedRowInComponent:0];

    //根据索引找到对应的省份

    NSString *province=[[_dicData allKeys]objectAtIndex:selectProvinceIndex];

    //根据省份找到对应的城市

    _cityArr=[[NSMutableArray alloc]init];

    _cityArr=[_dicData objectForKey:province];

    

    NSLog(@"%@",_dicData);

    

    

}

//UIPickerViewDataSource

//列个数

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

    NSLog(@"allKeys=%ld",[_dicData allKeys].count);

    return _provinceArr.count;

    

}

//行个数

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component

{

    if (component==0) {

        return _provinceArr.count;

    }

    else

    {

        return _cityArr.count;

    }

    

}

//UIPickerViewDelegate

//每列宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component

{

    return _pickerView.frame.size.width/2;

}

//每列中行的宽度

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component

{

    if (component==0) {

        return 50;

    }

    return 30;

}

//填充每行内容

//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component

//{

//    if (component==0) {

//        return [_provinceArr objectAtIndex:row];

//    }

//    else{

//        return [_cityArr objectAtIndex:row];

//    }

//}





//为没行设置文本设置属性

- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component

{

    NSString *str=[[NSString alloc]init];

    if (component==0) {

        str=[_provinceArr objectAtIndex:row];

    }

    else{

        str=[_cityArr objectAtIndex:row];

    }

    NSMutableAttributedString *attributedString=[[NSMutableAttributedString alloc]initWithString:str];

    UIFont *font = [UIFont systemFontOfSize:10];

    [attributedString addAttribute:NSFontAttributeName value:font range:NSMakeRange(0, str.length)];

    [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor redColor] range:NSMakeRange(0, str.length)];

    return attributedString;

    

}

//当picker view需要给指定的component.row指定view时,调用此函数.返回值为用作row内容的view  可以自定义View

//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

//{

//    UILabel *label=[[UILabel alloc]init];

//    label.text=@"text";

//    label.backgroundColor=[UIColor redColor];

//    return label;

//    

//}



//选中某一轮子的某一列

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component

{

    if (component==0) {

        NSString *selectProvince=[_provinceArr objectAtIndex:row];

        _cityArr=[_dicData objectForKey:selectProvince];

        //重新加载某一个轮子

        [_pickerView reloadComponent:1];

        //选中轮子的某一行

        [_pickerView selectRow:0 inComponent:1 animated:YES];

        

    }

    else

    {

        if (row==0) {

            return;

        }

        NSInteger selectProvinceIndex=[_pickerView selectedRowInComponent:0];

        NSString *province=[_provinceArr objectAtIndex:selectProvinceIndex];

        NSString *city=[_cityArr objectAtIndex:row];

        NSLog(@"%@-----%@",province,city);

    }

}



- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end

 UIKit 框架之UIPickerView

你可能感兴趣的:(view)