Uipickerview the development of general selection of areas or hierarchical data will be used, similar to the front end of the tree structure, but the PC is generally from the top to the bottom of the classification, using Uipickview is from left to right implementation, you can dynamically set the row column data in Uipickview, Display the results in a text input box and simply define the data source. Capturing events by declaring an agreement, showing the results, is relatively simple, and goes to the chase.
Page Layout
The page layout is relatively simple, a uipickerview, a text box:
The declaration in the header file implements the Uipickerviewdelegate,uipickerviewdatasource protocol:
@interface Viewcontroller:uiviewcontroller <UIPickerViewDelegate,UIPickerViewDataSource> @property (Weak, nonatomic) Iboutlet Uipickerview *pickview; @property (weak, nonatomic) Iboutlet Uitextfield *areatextfield; @end
Demo Implementation
Define an array of stored data:
@interface Viewcontroller () { nsarray *areaarr; Nsmutablearray *teamarr;}
Initialize data:
-(void) viewdidload { [super viewdidload]; Additional setup after loading the view, typically from a nib. [Email protected] [@ "South West District", @ "Central area", @ "Southeast", @ "Atlantic area" @ "North West District" @ "Pacific"]; Teamarr=[[nsmutablearray alloc] init]; [Teamarr addobject:@[@ "Spurs", @ "Grizzly Bear", @ "calf", @ "Rocket", @ "Pelican"]; [Teamarr addobject:@[@ "piston", @ "Walker", @ "Knight", @ "Bull", @ "stag"]; [Teamarr addobject:@[@ "Heat", @ "Magic", @ "Eagle", @ "Wizards", @ "Hornets"]; [Teamarr addobject:@[@ "Celtic", @ "76 people", @ "Knicks", @ "Basket net" @ "dragon"]; [Teamarr addobject:@[@ "Forest Wolf", @ "nuggets", @ "Jazz", @ "Pioneer", @ "Thunder"]; [Teamarr addobject:@[@ "King", @ "Sun", @ "Lakers", @ "Clippers", @ "Warrior"];}
To set the number of columns for Pickerview:
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{ return 2;}
Number of rows returned:
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{ //Judging column if (component==0) { return [Areaarr count]; } else{ //Determine the current line number in column 0 nsinteger currentrow=[pickerview selectedrowincomponent:0]; return [Teamarr[currentrow] count];} }
Set the data in each column of each row:
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component{ if (component==0) { //o column from the range array, select return areaarr[row]; } else{ //According to the line number selected in column 0 nsinteger currentrow=[pickerview selectedrowincomponent:0]; return teamarr[currentrow][row];} }
Select the event after completion:
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (nsinteger) component{ [ Pickerview Reloadcomponent:1]; Nsinteger Arearow=[pickerview selectedrowincomponent:0]; Nsinteger Teamrow=[pickerview selectedrowincomponent:1]; [_areatextfield settext:[nsstring stringwithformat:@ "%@-%@", Areaarr[arearow],teamarr[arearow][teamrow]]; }
The specific results are as follows:
iOS development-Data selection Uipickerview