pickView的用法

来源:互联网 发布:杜冷丁多少钱一支淘宝 编辑:程序博客网 时间:2024/06/06 18:38

#import "PickerViewController.h"


@interface PickerViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>


@end


@implementation PickerViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

   self = [superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];

   if (self) {

        // Custom initialization

    }

    return self;

}


- (void)viewDidLoad

{

    [superviewDidLoad];

  

//    NSDictionary * dic1 = [NSDictionary dictionaryWithObjectsAndKeys:@"xiaoMao",@"name",@"18",@"age", nil];

//    NSLog(@"=====%d",[dic1 count]);

//    NSLog(@"=====%@",[dic1 allKeys]);

//    NSLog(@"=====%@",[dic1 allValues]);

//    

    

    NSArray *heNanArray =@[@"郑州",@"南阳",@"洛阳",@"许昌",@"开封"];

   NSArray *shanDongArray =@[@"济南",@"青岛",@"日照",@"烟台",@"菏泽"];

    NSArray *deZhouArray =@[@"拉斯维加斯",@"芝加哥",@"休斯顿",@"华盛顿"];

   NSArray *jiaZhouArray =@[@"洛杉矶",@"旧金山",@"西雅图"];

    

   NSDictionary * chinaDic =@{@"河南":heNanArray,@"山东":shanDongArray};

//    NSLog(@"chinaDic=====%d",[chinaDic count]);

    

   NSDictionary * usaDic =@{@"加州":jiaZhouArray,@"德州":deZhouArray};

    

   _countryArray =@[chinaDic,usaDic];

    

    //countryStrike 1.6

    //初始化一个选择器

   UIPickerView * picker = [[UIPickerViewalloc]initWithFrame:CGRectMake(10,100,300,100)];

    

    [pickersetDelegate:self];

    

    [pickersetDataSource:self];

    

    [self.viewaddSubview:picker];

    

}

#pragma mark 数据源方法

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView

{

   return3;

}

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

{

   if (component ==0)

    {

        //国家个数的数组

       return [_countryArraycount];

    }

   elseif (component ==1)

    {

        //分析;每个国家的省份数量不一致,根据我们选择第一列的国家来找到对应国家的省份数量

        //selectedRowInComponent 找到第0列被选择的国家的索引值

       NSInteger selectCountryIndex =[pickerViewselectedRowInComponent:0];

        

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

        

       NSDictionary * provinceDic = [_countryArrayobjectAtIndex:selectCountryIndex];

      

//        NSLog(@"doubi======%d",[provinceDic count]);

       return [provinceDiccount];

        

    }

   else

    {

        //找到第0列被选择行的索引值

       NSInteger selectCountryIndex =[pickerViewselectedRowInComponent:0];

        

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

       NSDictionary * provinceDic =_countryArray[selectCountryIndex];

        

        //找到Component1的这列,被选择行对应的索引值

       NSInteger selectProvinceIndex = [pickerViewselectedRowInComponent:1];

        //找到对应国家所有省份的城市

       NSArray * allCityAray = [provinceDicallValues];

//        NSLog(@"allCityAray======%d",[allCityAray count]);

        //找到对应省份所有的城市

       NSArray * selectCityArray  = allCityAray[selectProvinceIndex];

        

       return [selectCityArraycount];

        

    }

}



#pragma  mark 代理方法

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

{

   if (component ==0)

    {

       return row ==0?@"中国":@"美国";

    }

   elseif (component ==1)

    {

       NSInteger  selectCountry = [pickerViewselectedRowInComponent:0];

        //获得对应国家中所有的省份

       NSDictionary * province =_countryArray[selectCountry];

        //获得国家所有省份的名称

       NSArray * provinceNameArray = [provinceallKeys];

       return provinceNameArray[row];

        

        

    }

   else

    {

       NSInteger  countryIndex = [pickerViewselectedRowInComponent:0];

        

        // @{@"河南":heNanArray,@"山东":shanDongArray};

        

       //@{@"加州":jiaZhouArray,@"德州":deZhouArray};


       NSDictionary * provinceDic =_countryArray[countryIndex];


        //heNanArray && shanDongArray

        //jiaZhouArray && deZhouArray

       NSArray * allCityArray = [provinceDicallValues];

        

       NSInteger  provinceIndex = [pickerViewselectedRowInComponent:1];

        

       NSArray * selectArray = allCityArray[provinceIndex];

        

       return selectArray[row];

        

    }

   

}

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

{

    //点击选择以后 重新加载所有数据

    [pickerView reloadAllComponents];

//    if (component == 0)

//    {

//        [pickerView selectRow:0 inComponent:1 animated:YES];

//        [pickerView selectRow:0 inComponent:2 animated:YES];

//    }

//    else if (component == 1)

//    {

//        [pickerView selectRow:0 inComponent:2 animated:YES];

//    }

}

- (void)didReceiveMemoryWarning

{

    [superdidReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


/*

#pragma mark - Navigation


// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

{

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.

}

*/


@end


1 0
原创粉丝点击