Description: A Njprovince class has been prepared in advance, which declares 2 properties name and cities, and creates and implements a dynamic and static method for quickly creating classes. You can import directly.
0. Create a plist file, the root is an array, the dictionary (in the dictionary for the province and the city)
1. Load this plist file
1> declaring Array properties
@property (Nonatomic,strong) Nsarray *provinces;
2> lazy Loading (at the end of the implementation file)
#pragma mark-Lazy loading
-(Nsarray *) Provinces
{
if (_provinces = = nil) {
Load Resources
Nsarray *array = [Nsarray arraywithcontetnsoffile:[nsbundle mainbundle] pathforresource:@ "Cities.plist" Oftype:nil]];
Convert a dictionary to a model
Nsmutablearray *models = [Nsmutablearray arrayWithCapacity:array.count];
For (nsdcitionary *dict in array) {
Njprovince *province = [Njprovince provincewithdictionary:dict];
[Models Addobject:province];
}
_provinces = [models copy];
}
return _provinces;
}
1. In the Viewdidload
1. Create Pickerview
Pickerview has the default frame
Uipickerview = *pickerview = [Uipickerview alloc] init];
2. Set up the data source and proxy for the controller (and implement the relevant protocol)
Pickerview.datasource = self;
Pickerview.delegate = self;
[Self.view Addsubview:pickerview];
Self.pickerview = pickerview[Specific add reasons see below]
2. Implementing data source methods and proxy methods
#pragma mark-Data source methods
Tell the system how many columns
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview
{
return 2;
}
Note: To get Pickview, declare a weak property Uipickerview, and add Self.pickerview = Pickerview in Viewdidload,
//Tell the system how many rows
- (Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponents: (nsinteger) component
{
if (0 = = component) {
//province
return self.provinces.count;
} else {//city
//0. Gets the No. 0 column selected row
Nsinteger Selectinde x = [Self.pickerview selectedrowincomponent:0];
//1. Gets the corresponding province according to the number of rows selected in column No. 0 (Gets the model of the first column selected row)
Njprovince *province = Self.provinces[selectindex];
//2. Get the corresponding city of the corresponding province
Nsarray *cities = province.cities;
//3. Returns the number of cities
return Cities.count;
}
}
#pragma mark-Proxy method
Tell the system what each line shows
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponents: (Nsinteger) component
{
if (0 = = component) {
Provinces
return [Self.provinces[row] name];
} else {//City
0. Get the row selected in column No. 0
Nsinteger Selectindex = [Pickerview selectedrowincomponent:0];
1. Get the corresponding province
Njprovince *province = Self.provinces[selectindex];
2. Access to the corresponding city
return Province.cities[row];
}
}
The corresponding city is shown according to the selected province
Monitor the selection of Pickerview
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
Determine if the No. 0 column (province column) has been modified
if (0 = = component) {
Refresh data for column 1th
Pickerview Reloadcomponent:1];
Let 1th column scroll to line No. 0
[Pickerview selectrow:0 incomponent:1 Animated:yes];
}
iOS Foundation-uikit Framework-Advanced View-uipickerview-Instance 2: City Selection (column has a relationship to columns)