標籤:
*********
#import "ViewController.h"#import "Province.h"@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>@property (nonatomic,strong)NSArray *provinces;@property (nonatomic,assign)NSInteger indexOfProvice;//當前預設選中的省份@end@implementation ViewController-(NSArray *)provinces{ if (!_provinces) { _provinces = [Province provinceList]; } return _provinces;}- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. NSLog(@"%@",self.provinces);}// returns the number of ‘columns‘ to display.- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 2;}// returns the # of rows in each component..- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ if (component == 0) {//省分 return self.provinces.count; } //擷取對應省份的城市個數 Province *province = self.provinces[self.indexOfProvice]; return province.cities.count;}#pragma mark 顯示資料-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{ if(component == 0){//顯示省份的名字 //對應列行的省份 Province *province = self.provinces[row]; return province.name; } //擷取選中的城市,顯示城市名字 Province *selectedProvice = self.provinces[self.indexOfProvice]; return selectedProvice.cities[row];}//-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{// // UILabel *label = (UILabel *)view;// if (!label) {// label = [[UILabel alloc] init];// }// // if(component == 0){//顯示省份的名字// //對應列行的省份// Province *province = self.provinces[row];// label.text = province.name;// label.backgroundColor = [UIColor grayColor];// }else{// // //擷取選中的城市,顯示城市名字// Province *selectedProvice = self.provinces[self.indexOfProvice];// label.text = selectedProvice.cities[row];// label.backgroundColor = [UIColor blueColor];// }// // // // // return label;//}#pragma mark 選中行-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{ if (component == 0) {//第一列省的選中改變後,就要更新第二列資料 //更新選中省份的索引 self.indexOfProvice = row; //重新整理資料 //全部重新整理 //[pickerView reloadAllComponents]; //部份重新整理 [pickerView reloadComponent:1]; //不管之前第二列選中第幾行,重新重新整理資料後,都顯示每二列的第一行 [pickerView selectRow:0 inComponent:1 animated:YES]; }}#pragma mark 設定寬度-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{ if (component == 0) { return 80; } return 200;}@end
***model.m
#import "Province.h"@implementation Province-(instancetype)initWithDict:(NSDictionary *)dict{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dict]; } return self;}+(instancetype)provinceWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict];}+(NSArray *)provinceList{ //plist檔案路徑 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"provinces.plist" ofType:nil]; NSArray *provincePlist = [NSArray arrayWithContentsOfFile:filePath]; NSMutableArray *provinceM = [NSMutableArray array]; for (NSDictionary *dic in provincePlist) { Province *prov = [Province provinceWithDict:dic]; [provinceM addObject:prov]; } return provinceM;}@end
****model.h
#import <UIKit/UIKit.h>@interface Province : NSObject@property(nonatomic,copy)NSString *name;@property(nonatomic,strong)NSArray *cities;-(instancetype)initWithDict:(NSDictionary *)dict;+(instancetype)provinceWithDict:(NSDictionary *)dict;+(NSArray *)provinceList;@end
IOS第11天(3:UIPickerView省市聯動)