//从目录获取已经写好的数据(city.json)
NSString* filePath = [FileUtil documentsPathWithFileName:@"city.json"];
if (![FileUtil fileExists:filePath])
{
filePath = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"json"];
}
NSString* fileContent = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSDictionary* dataDic = [fileContent objectFromJSONString];
NSArray* proviceArray=[dataDic objectForKey:@"proviceList"];
NSDictionary* cityMapDic=[dataDic objectForKey:@"cityMap"];
// NSLog(@"proviceArray = %@",proviceArray);
// NSLog(@"cityMapDic = %@",cityMapDic);
NSUInteger count=[proviceArray count];
cityList_ = [NSMutableDictionary dictionaryWithCapacity:count];
NSString* key=nil;
NSString* value=nil;
NSArray* cityDicArray=nil;
NSMutableArray* cityNameArray=nil;
for (NSDictionary* proviceDic in proviceArray)
{
key=[proviceDic objectForKey:@"value"];
value=[proviceDic objectForKey:@"text"];
cityDicArray=[cityMapDic objectForKey:key];
cityNameArray=[NSMutableArray arrayWithCapacity:[cityDicArray count]];
for (NSDictionary* cityDic in cityDicArray)
{
[cityNameArray addObject:[cityDic objectForKey:@"text"]];
}
[cityList_ setValue:cityNameArray forKey:value];
}
创建PickerView
-(void)openPickerView
{
//select service
[self.view endEditing:YES];
CGRect rect=self.view.bounds;
PickerView* pickerView=[[PickerView alloc] initWithFrame:rect delegate:self];
pickerView.backgroundColor = DefaultBGColor;
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
{
UILabel* bgLabel = [[UILabel alloc]initWithFrame:rect];
bgLabel.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
bgLabel.tag = 12310;
bgLabel.userInteractionEnabled = YES;
[self.view addSubview:bgLabel];
CGRect tempRect = pickerView.frame;
tempRect.origin.y = self.view.frame.size.height - pickerView.frame.size.height;
pickerView.frame = tempRect;
[self.view addSubview:pickerView];
}
else
{
UIActionSheet* actionSheet=[[UIActionSheet alloc] initWithTitle:@""
delegate:self
cancelButtonTitle:nil
destructiveButtonTitle:nil
otherButtonTitles:nil];
[actionSheet addSubview:pickerView];
[actionSheet showInView:self.view];
[actionSheet setBounds:CGRectMake(0, 0, 320, 520)];
}
}
协议:
-(void)PickerViewOnConfirm:(PickerView*)view
{
NSInteger index=[view.pickerView selectedRowInComponent:0];
if(selectState_==1)
{
UIButton* temp = (UIButton*)[self.view viewWithTag:12302];
[temp setTitle:[[cityList_ allKeys] objectAtIndex:index] forState:UIControlStateNormal];
UIButton* tempsubs = (UIButton*)[self.view viewWithTag:12303];
[tempsubs setTitle:@"请选择城市" forState:UIControlStateNormal];
selectCity_ = @"请选择城市";
selectProvince_ = [[cityList_ allKeys] objectAtIndex:index];
}
else
{
NSArray* city = [cityList_ objectForKey:selectProvince_];
UIButton* temp = (UIButton*)[self.view viewWithTag:12303];
[temp setTitle:[city objectAtIndex:index] forState:UIControlStateNormal];
selectCity_ = [city objectAtIndex:index];
}
}
-(void)PickerViewOnClose:(UIView*)view
{
if ([UIDevice currentDevice].systemVersion.floatValue >= 8.0)
{
[view removeFromSuperview];
UILabel* bg = (UILabel*)[self.view viewWithTag:12310];
[bg removeFromSuperview];
}
else
{
[(UIActionSheet*)view.superview dismissWithClickedButtonIndex:0 animated:YES];
}
}
- (NSInteger)numberOfRowsInComponent:(NSInteger)component
{
if(selectState_==1)
{
return [cityList_ allKeys].count;
}
NSArray* city = [cityList_ objectForKey:selectProvince_];
return city.count;
}
- (NSInteger)numberOfComponents
{
return 1;
}
- (NSString*)titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
if(selectState_==1)
{
return [[cityList_ allKeys] objectAtIndex:row];
}
NSArray* city = [cityList_ objectForKey:selectProvince_];
return [city objectAtIndex:row];
}
- (CGFloat)widthForComponent:(NSInteger)component
{
return 200;
}
- (void)PickerView:(PickerView*)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
[pickerView.pickerView selectRow:row inComponent:component animated:YES];
}