UI Foundation--uipikcerview implementation of provincial and municipal linkage example

Source: Internet
Author: User

Requirements:1. Display 2 columns of data, one column of data for the provincial city, and the other as the provincial counterparts in the city-level cities, and to choose from; 2. When selecting provincial cities, the corresponding column shows the city-level cities to which they are saved, and optionally; 3. When the provincial cities are re-selected, the corresponding city-level cities show City-level cities are not indexed at 0); implementation steps:1, build the interface, 2, the implementation of Uipikcerview agent method, 3, response to select events, 4, complete display selection data; Specific code:Model:
1 //2 //JWProvince.h3 //12-27-province4 //5 //Created by Xiaomoge on 14/12/27.6 //Copyright (c) 2014 Xiaomoge. All rights reserved.7 //8 9 #import<Foundation/Foundation.h>Ten  One @interfaceJwprovince:nsobject A@property (nonatomic,copy) NSString *name; -@property (nonatomic,strong) Nsarray *cities; --(Instancetype) Initwithdic: (Nsdictionary *) dic; the+ (Instancetype) Provincewithdic: (Nsdictionary *) dic; -+ (Nsmutablearray *) provincelist; - @end

1 //2 //JWPROVINCE.M3 //12-27-province4 //5 //Created by Xiaomoge on 14/12/27.6 //Copyright (c) 2014 Xiaomoge. All rights reserved.7 //8 9 #import "JWProvince.h"Ten  One @implementationjwprovince A-(Instancetype) Initwithdic: (Nsdictionary *) dic { -     if(self =[Super Init]) { - [self setvaluesforkeyswithdictionary:dic]; the     } -     returnSelf ; - } -+ (Instancetype) Provincewithdic: (Nsdictionary *) dic { +     return[[Self alloc] initwithdic:dic]; - } +  A+ (Nsmutablearray *) provincelist { atNsarray *array = [Nsarray arraywithcontentsoffile:[[nsbundle mainbundle] Pathforresource:@"Provinces"OfType:@"plist"]]; -      -Nsmutablearray *temparray =[Nsmutablearray array]; -      -      for(Nsdictionary *dicinchArray) { -Jwprovince *province =[Jwprovince provincewithdic:dic]; in [Temparray addobject:province]; -     } to     returnTemparray; + } - @end

Controller:

1 //2 //VIEWCONTROLLER.M3 //12-27-province4 //5 //Created by Xiaomoge on 14/12/27.6 //Copyright (c) 2014 Xiaomoge. All rights reserved.7 //8 9 #import "ViewController.h"Ten #import "JWProvince.h" One @interfaceViewcontroller () <UIPickerViewDelegate,UIPickerViewDataSource> A@property (nonatomic,strong) Nsarray *Provincedatas; - @property (nonatomic,assign) Nsinteger index; - @end the  - @implementationViewcontroller - #pragmaMark-Lazy Loading --(Nsarray *) Provincedatas { +     if(!_provincedatas) { -_provincedatas =[Jwprovince provincelist]; +     } A     return_provincedatas; at } - #pragmaMark-uipickerview Data source - //returns the number of columns that display data --(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview { -     return 2; - } in //returns the number of corresponding rows in each column --(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) component { to     if(Component = =0) { +         returnSelf.provinceDatas.count;//number of columns displayed in the first column -     } theJwprovince *pro =Self.provincedatas[self.index]; *     returnPro.cities.count;//number of columns shown in the second column $ }Panax Notoginseng #pragmaMark-uipickerview Agent - //returns the contents of each column and each row displayed the-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component { +     if(Component = =0) { AJwprovince *pp =Self.provincedatas[row]; the         returnPp.name;//what is displayed in the first column +     } -Jwprovince *pro =Self.provincedatas[self.index]; $     returnPro.cities[row];//The second column shows the content $ } - #pragmaHow to select Mark-pickerview - //actions when a row is selected the- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component { -     if(Component = =0) {WuyiSelf.index =Row; the[Pickerview reloadcomponent:1];//Reload the corresponding data -[Pickerview reloadallcomponents];//Reload all the data Wu[Pickerview SelectRow:0Incomponent:1Animated:yes];//The first row of every two columns is displayed after re-refreshing the data, regardless of whether the second column is selected in the previous row -     } About } $ @end

UI Foundation--uipikcerview implementation of provincial and municipal linkage example

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.