1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// ModifyShippingAddressView.m
// Lighting
//
// Created by 曹云霄 on 16/5/5.
// Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//
#import "ModifyShippingAddressView.h"
@implementation ModifyShippingAddressView
{
NSMutableArray *provinces; //城市大数组
NSArray *cities; //城市小数组
NSString *provincesString; //省份
NSString *cityString; //城市
NSString *cityIDstring; //城市ID
NSString *_cityCode; //城市编码
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (void)awakeFromNib
{
[super awakeFromNib];
[self uiConfigAction];
}
#pragma mark -UI
- (void)uiConfigAction
{
self.cityPickerView.delegate = self;
self.cityPickerView.dataSource = self;
NSString *path = [[NSBundle mainBundle] pathForResource:@"city" ofType:@"json"];
NSString *string = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
provinces = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
cities = [[provinces objectAtIndex:0] objectForKey:@"list"];
}
#pragma mark -城市选择器代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)componen
{
switch (componen) {
case 0:
{
return [provinces count];
}
break;
case 1:
{
return [cities count];
}
break;
default:
break;
}
return 0;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
switch (component)
{
case 0:
return [[provinces objectAtIndex:row] objectForKey:@"name"];
break;
case 1:
return [[cities objectAtIndex:row] objectForKey:@"name"];
break;
default:
return nil;
break;
}
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
switch (component)
{
case 0:
cities = [[provinces objectAtIndex:row] objectForKey:@"list"];
[self.cityPickerView selectRow:0 inComponent:1 animated:NO];
[self.cityPickerView reloadComponent:1];
provincesString = [[provinces objectAtIndex:row] objectForKey:@"name"];
cityString = [[cities objectAtIndex:0] objectForKey:@"name"];
_cityCode = [[cities objectAtIndex:0] objectForKey:@"code"];//城市编码
break;
case 1:
cityString = [[cities objectAtIndex:row] objectForKey:@"name"];
cityIDstring = [[cities objectAtIndex:row] objectForKey:@"code"];
_cityCode = [NSString stringWithFormat:@"%@",[[cities objectAtIndex:row] objectForKey:@"code"]];//城市编码
break;
default:
break;
}
if ([self.delegate respondsToSelector:@selector(citySelected:WithprovincesString:)]) {
[self.delegate citySelected:cityString WithprovincesString:provincesString];
}
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
UILabel* pickerLabel = (UILabel*)view;
if (!pickerLabel){
pickerLabel = [[UILabel alloc] init];
pickerLabel.adjustsFontSizeToFitWidth = YES;
pickerLabel.textColor = kMainBlueColor;
pickerLabel.textAlignment = NSTextAlignmentCenter;
[pickerLabel setBackgroundColor:[UIColor clearColor]];
[pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
}
// Fill the label text here
pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
return pickerLabel;
}
@end