Example explains the usage of Uipickerview scrolling selection bar in IOS application development _ios

Source: Internet
Author: User

Basis
1.UIPickerView Properties

Data source (used to tell Uipickerview how many rows are in columns)

Copy Code code as follows:

@property (nonatomic,assign) ID dataSource;


Agent (used to tell Uipickerview what to display every 1 lines per 1 column, monitor Uipickerview selection)
Copy Code code as follows:

@property (nonatomic,assign) ID delegate;


Whether to display the selected indicator
Copy Code code as follows:

@property (nonatomic) BOOL showsselectionindicator;


How many columns are there?
Copy Code code as follows:

@property (nonatomic,readonly) Nsinteger numberofcomponents;

2.UIPickerView method

Re-refresh all columns

Copy Code code as follows:

-(void) reloadallcomponents;

Re-refresh the component column

Copy Code code as follows:

-(void) Reloadcomponent: (Nsinteger) component;

Actively select the row of the first component column

Copy Code code as follows:

-(void) SelectRow: (Nsinteger) Row incomponent: (Nsinteger) component animated: (BOOL) animated;

Get the currently selected line number for the component column

Copy Code code as follows:

-(Nsinteger) Selectedrowincomponent: (Nsinteger) component;

3.UIPickerView Data Source Method

How many columns are there?

Copy Code code as follows:

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview;

How many rows are there in the component column?
Copy Code code as follows:

-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component;

4.UIPickerView Proxy method
What is the width of the component column?

Copy Code code as follows:

-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (nsinteger) component;

What is the row height of the component column?
Copy Code code as follows:

-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component;

What text is displayed on row component column

Copy Code code as follows:

-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component;

What view (content) is displayed in row rows of column component
Copy Code code as follows:

-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view;

Row component column of Pickerview is selected
Copy Code code as follows:

-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component;

Instance
Uipickerview as a common control of iOS I believe we all have this demand.
Today we will simply create a:
New project Name: Testuipickerview
Create a uipickerview in the default generated Viewcontroller
First, create in the Viewdidload method

Copy Code code as follows:

-(void) Viewdidload {
[Super Viewdidload];
Do no additional setup after loading the view, typically from a nib.

Selection box
Uipickerview *pickerview = [[Uipickerview alloc] initwithframe:cgrectmake (0, 100, 320, 216)];
Show check box
Pickerview.showsselectionindicator=yes;
Pickerview.datasource = self;
Pickerview.delegate = self;
[Self.view Addsubview:pickerview];

_protimelist = [[Nsarray alloc]initwithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "ten", nil];
_protitlelist = [[Nsarray alloc]initwithobjects:@ "1", @ "2", @ "3", @ "4", @ "5", @ "6", @ "7", @ "8", @ "9", @ "ten", nil];


}

Then, we create the associated proxy method
Uipickerviewdatasource related Agent
Copy Code code as follows:

#pragma Mark--Uipickerviewdatasource
Number of Pickerview columns
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview {
return 2;
}

Number of Pickerview per column
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (Nsinteger) Component {
if (component = = 0) {
return [_protitlelist Count];
}

return [_protimelist Count];
}

Uipickerviewdelegate Related Proxy method
Copy Code code as follows:

#pragma Mark--uipickerviewdelegate
Width per column
-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (Nsinteger) Component {

if (component = = 1) {
return 40;
}
return 180;
}
Returns the selected row
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component
{
if (component = = 0) {
NSString *_pronamestr = [_protitlelist objectatindex:row];
NSLog (@ "namestr=%@", _pronamestr);
} else {
NSString *_protimestr = [_protimelist objectatindex:row];
NSLog (@ "_protimestr=%@", _protimestr);
}

}

Returns the contents of the current row, where the values in the array are added to the scrolling display bar
-(nsstring*) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component
{
if (component = = 0) {
return [_protitlelist Objectatindex:row];
} else {
return [_protimelist Objectatindex:row];

}
}

Once you have completed the above code, we can run the Project view effect
The following figure:

Related Article

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.