实现PickerView的数据初始化
1 //确定pickerVIew轮子的个数 2 -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView 3 { 4 return 2; 5 } 6 //确定pickerView的每个轮子的item数 7 -(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component 8 { 9 if (component == 0) {//省份个数 10 return [provinceArray count]; 11 } 12 else //市的个数 13 { 14 return [cityArray count]; 15 } 16 } 17 //确定每个轮子的每一项显示什么内容 18 #pragma mark 实现协议UIPickerViewDelegate方法 19 -(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component 20 { 21 if (component == 0) {//选择省份名 22 return [provinceArray objectAtIndex:row]; 23 } 24 else//选择市名 25 { 26 return [cityArray objectAtIndex:row]; 27 } 28 }
1 //加载数据plist里面的数据 2 NSString *plistPath = [[NSBundle mainBundle]pathForResource:@"province" ofType:@"plist"]; 3 //获取属性列表文件中的全部数据 4 NSDictionary *dict = [[NSDictionary alloc]initWithContentsOfFile:plistPath]; 5 self.pickerData = dict; 6 7 //省份名数据 8 self.pickerProvincesData = [self.pickerData allKeys]; 9 10 //默认取出第一个省的所有市的数据 11 NSString *seletedProvince = [self.pickerProvincesData objectAtIndex:0]; 12 self.pickerCitiesData = [self.pickerData objectForKey:seletedProvince]; 13 14 self.pickerView.dataSource = self; 15 self.pickerView.delegate = self;