iOS-中國省市區plist(續)UIPickerView的應用
上一篇文章將plist檔案中的中國省市區讀取後,儲存為json字串打印出來後沒有錯誤,現在再將其資料利用UIPickerView展示。
1,以前運用UIPickerView一般是年月日,基本上資料都是死陣列組成,例如每年就12個月 ,一個月天數也就28,28,30或者31,這樣的話寫起來應該不會麻煩;
2,省市區對照表中,每個省對應的市不同,每個市對應的區也不同,這就導致在填充資料時會動態改變,這個時候要利用UIPickerView的代理方法和其介面方法來實現
首先新增一個uipickerview為子view,設定代理為self,這樣才可以對其填充資料和相應操作,類似uitableview。
然後實現uipickerview的代理方法,在選中某一組的某一行時更新其後邊的一組資料
-(void)addpickerview
{
mypiker=[[UIPickerViewalloc]initWithFrame:CGRectMake(0, 200,320, 300)];
mypiker.delegate=self;
mypiker.dataSource=self;
mypiker.showsSelectionIndicator=YES;
[self.view addSubview:mypiker];
carray=[[NSMutableArrayalloc]initWithArray:[PdicobjectForKey:[parrayobjectAtIndex:
if ([[carrayobjectAtIndex:0] isKindOfClass:[NSStringclass]]) {
return;
}else{
ddictionary=[NSMutableDictionarydictionaryWithDictionary:[carrayobjectAtIndex:0]];
NSArray * array=[[ddictionary allValues] objectAtIndex:0];
dearray=[[NSMutableArray alloc]initWithArray:array];
NSLog(@"%d",dearray.count);
}
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 3;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
if (component==0) {
return parray.count;
}else if(component==1){
return carray.count;
}else if(component==2){
NSLog(@"%d",dearray.count);
return dearray.count;
}
return YES;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 44;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if (component==0) {
return [parray objectAtIndex:row];
}else if (component==1) {
if ([[carrayobjectAtIndex:row] isKindOfClass:[NSStringclass]]) {
return [carray objectAtIndex:row];
}else{
return [[[carrayobjectAtIndex:row] allKeys] objectAtIndex:0];
}
}
else{
return [dearray objectAtIndex:row];
}
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSLog(@"%d----%d",row,component);
if (component==0) {
carray=[[NSMutableArrayalloc]initWithArray:[PdicobjectForKey:[parrayobjectAtIndex:row]]];
if ([[carrayobjectAtIndex:0] isKindOfClass:[NSDictionaryclass]]) {
ddictionary=[carray objectAtIndex:0];
dearray=[[NSMutableArrayalloc]initWithArray:[[ddictionaryallValues] objectAtIndex:0]];
}
else{
[dearray removeAllObjects];
[dearray addObject:@"無"];
}
[mypikerreloadComponent:1];
[mypikerreloadComponent:2];
}
else if (component==1){
if ([[carrayobjectAtIndex:row] isKindOfClass:[NSDictionaryclass]]) {
ddictionary=[carray objectAtIndex:row];
dearray=[[ddictionaryallValues] objectAtIndex:0];
}else{
[dearray removeAllObjects];
[dearray addObject:@"無"];
NSLog(@"沒有區域選項");
}
[mypikerreloadComponent:2];
}
}