iOS development-Data selection Uipickerview

Source: Internet
Author: User

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

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.