Simple application of Uipickerview--the realization of province/city choice
To achieve the effect, the left is the province selection, right select the province corresponding to the city
Data plist Form
Project: Project Download
Https://github.com/Nongchaozhe/UIPickerView-Province-city
The implementation of Uipickerview is important or the method of two agent protocol
- (void) Viewdidload {[SuperViewdidload]; [ SelfLoadData]; _pickview = [[Uipickerview alloc] Initwithframe:cgrectmake ( -, -, -, -)]; _pickview. DataSource= Self; _pickview. Delegate= Self; [ Self. ViewAddsubview:_pickview];}#pragma mark-loaddata- (void) LoadData {NSBundle*bundle = [NSBundleMainbundle];NSString*path = [Bundle pathforresource:@"City"oftype:@"Plist"]; _dataarray = [NsmutablearrayArraywithcontentsoffile:path]; _citiesarray = _dataarray[0][@"Cities"];}
Implementation of the Proxy method:
#pragma mark - dataSource//注意这里是几列的意思。我刚刚开始学得时候也在这里出错,没理解- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return2;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { if0) { return _dataArray.count; }else { return _citiesArray.count; }}
#pragma mark-delegate- (CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (Nsinteger) Component {return Max;} - (CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (Nsinteger) Component {return -;}//Returns the content displayed for each row- (NSString*) Pickerview: (Uipickerview *) Pickerview Titleforrow: (Nsinteger) Row forcomponent: (Nsinteger) Component {if(Component = =0) {return[NSStringstringwithformat:@"%@", _dataarray[row][@"State"]]; }Else{return[NSStringstringwithformat:@"%@", _citiesarray[row]]; }}//When changing provinces, reload the 2nd column of data, partially loading- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (Nsinteger) Row incomponent: (Nsinteger) Component {if(Component = =0) {_citiesarray = _dataarray[row][@"Cities"]; [_pickview reloadcomponent:1]; }}//Supplemental Instructions ~ Sometimes we need to show the view, if the implementation of the method, then the @selector (pickerView:attributedTitleForRow:forComponent:) will not be called, So if the selector has both text and pictures, you can select the text area to return to Uilabel. (My solution ~ may have better ~)//-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Uipickerview Simple Application--province/city selection implementation