iOS Development Uipickerview Common attribute methods

Source: Internet
Author: User

//

Viewcontroller.m

Uipickerviewall

#import "ViewController.h"

@interface Viewcontroller ()

@end

@implementation Viewcontroller

/*

Methods and properties commonly used by Uipickview controls:

(1)-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview; Returns the number of columns of Pickerview

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

Returns the number of rows corresponding to the component column of Pickview

(3)-(Nullable NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) Component

Returns the contents of each row in each column

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

The method is called when the user selects a column and a row of Pickview

(5)-(Nullable nsattributedstring *) Pickerview: (Uipickerview *) Pickerview Attributedtitleforrow: (nsinteger) row Forcomponent: (Nsinteger) component

Modify the style of the text in the row row of the component column in Pickview

(6)-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (Nullable UIView *) View the UIView control returned by the method will directly act as a Uipickview column for the component columns of the row row list item

(7)-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component set row height for component column

(8)-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (Nsinteger) component

This method sets the width of the component column

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

This method sets the Uipickview component column of the row row item, and the last parameter animated represents whether or not to use the animation

(a) @property (nonatomic,readonly) Nsinteger numberofcomponents;

Gets Uipickerview the number of list items contained in the specified column, which is read-only

*/

Simple and practical

-(void) viewdidload

//{

[Super Viewdidload];

//

Get the data you need to show

[Self loaddata];

//

Initialize Pickerview

Self.pickerview = [[Uipickerview alloc]initwithframe:cgrectmake (0, Self.view.bounds.size.width, 200)];

[Self.view AddSubview:self.pickerView];

//

Specifying data sources and delegates

Self.pickerView.delegate = self;

Self.pickerView.dataSource = self;

//}

#pragma mark loads the data

-(void) LoadData

//{

The data that needs to be presented is saved as an array

Self.letter = @[@ "AAA" @ "BBB" @ "CCC" @ "DDD"];

Self.number = @[@ "111", @ "222", @ "333", @ "444"];

//}

//

#pragma mark Uipickerview DataSource method Data Source methods

//

Specify Pickerview have several dials

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview

//{

Return 2;//first display letter, second display number

//}

//

Specify a few rows of data on each dial

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

//{

Nsinteger result = 0;

Switch (component) {

Case 0:

result = self.letter.count;//returns several rows of data based on the number of elements in the array

Break

Case 1:

result = Self.number.count;

Break

//

Default

Break

//    }

//

return result;

//}

//

#pragma mark Uipickerview Delegate method Proxy Methods

//

Specify how each row shows the data (similar to TableView here)

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

//{

NSString * title = nil;

Switch (component) {

Case 0:

title = Self.letter[row];

Break

Case 1:

title = Self.number[row];

Break

Default

Break

//    }

//

return title;

//}

#pragma mark Two linkage

-(void) viewdidload

{

[Super Viewdidload];

Loading data

[Self loaddata];

Specifying delegates

Self.pickerView.delegate = self;

Self.pickerView.dataSource = self;

}

Loading data

-(void) LoadData

{

NSString * Path = [[NSBundle mainbundle]pathforresource:@ "area" oftype:@ "plist"];

Self.provinces = [Nsarray Arraywithcontentsoffile:path];

Self.cities = [Nsarray arraywitharray:self.provinces[0][@ "Cities"];

Layout Constraints for labels

Self.label = [[UILabel Alloc]initwithframe:cgrectzero];

Self.label.translatesAutoresizingMaskIntoConstraints = NO;

Self.label.backgroundColor = [Uicolor Grouptableviewbackgroundcolor];

Self.label.textColor = [Uicolor Greencolor];

Self.label.font = [Uifont systemfontofsize:30];

[Self.label Settextalignment:nstextalignmentcenter];

[Self.view AddSubview:self.label];

Nsarray * Labeltop = [Nslayoutconstraint constraintswithvisualformat:@ "V:[_pickerview]-30-[_label ()]" options:0 Metrics:nil views:nsdictionaryofvariablebindings (_pickerview,_label)];

Nsarray * Labelh = [nslayoutconstraint constraintswithvisualformat:@ "h:|-20-[_label]-20-|" options:0 metrics:nil views : Nsdictionaryofvariablebindings (_label)];

[Self.view Addconstraints:labeltop];

[Self.view ADDCONSTRAINTS:LABELH];

}

There are several dials (component)

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview

{

return 2;

}

No dial with a few rows of data (rows)

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

{

Nsinteger rows = 0;

Switch (component) {

Case 0:

rows = self.provinces.count;//Returns rows based on data from the plist file

Break

Case 1:

rows = Self.cities.count;

Break

Default

Break

}

return rows;

}

Title of each row

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

{

NSString * title = nil;

Switch (component) {

Case 0:

title = self.provinces[row][@ "State"];

Break

Case 1:

title = self.cities[row][@ "City"];

Break

Default

Break

}

return title;

}

The delegate method of the callback when selected, in which the linkage between provinces and cities is achieved

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

{

Switch (component) {

Case 0://Select the province dial, change the city array according to the value of row, refresh the city array, realize linkage

Self.cities = self.provinces[row][@ "Cities"];

[Self.pickerview reloadcomponent:1];

Break

Case 1:

Self.label.text = [NSString stringwithformat:@ "%@%@", Self.provinces[[self.pickerview selectedrowincomponent:0]][@ " State "],self.cities[[self.pickerview selectedrowincomponent:1]][@" City "]];//if the second one is selected

Break

Default

Break

}

}

Modify the title style of Pickerview

#pragma Mark Pickerview Method

-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview

//{

Return 1;//dial Number

//}

//

Determine which pickerview to return to the corresponding rows

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

//{

Nsinteger rows = 0;

//

if (Pickerview = = Self.pickerview)

//    {

rows = Self.pickerViewArr.count;

//    }

Else

//    {

rows = Self.pickerViewArr2.count;

//    }

//

return rows;

//}

//

Determine which pickerview to return to the corresponding title

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

//{

NSString *str = nil;

//

if (Pickerview = = Self.pickerview)

//    {

str = Self.pickerviewarr[row];

//    }

Else

//    {

str = Self.pickerviewarr2[row];

//    }

return str;

//}

//

#pragma mark set font size and color for Pickerview

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

//{

You can customize the way your labels reach your custom Pickerview presentation data

uilabel* Pickerlabel = (uilabel*) view;

//

if (!pickerlabel)

//    {

Pickerlabel = [[UILabel alloc] init];

Pickerlabel.adjustsfontsizetofitwidth = YES;

Pickerlabel.textalignment = Nstextalignmentcenter;

[Pickerlabel Setbackgroundcolor:[uicolor Lightgraycolor];

[Pickerlabel Setfont:[uifont boldsystemfontofsize:15];

//    }

//

Pickerlabel.text=[self Pickerview:pickerview Titleforrow:row forcomponent:component];//Call the previous delegate method to get the title to show

return Pickerlabel;

//}

Check the callback method after a row to get the selected result

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

//{

if (Pickerview = = Self.pickerview)

//    {

Self.pickerviewselect = Self.pickerviewarr[row];

NSLog (@ "selected = =%@", self.pickerviewselect);

//    }

Else

//    {

Self.pickerviewselect2 = Self.pickerviewarr2[row];

//    }

//}

From Link: http://www.jianshu.com/p/811882ba8d78

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

Dispose of any resources the can be recreated.

}

@end

iOS Development Uipickerview Common Properties method

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.