One: Data source method
/** Data source protocol, all methods must be implemented */
@protocol uipickerviewdatasource<nsobject>
@required
Returns the number of columns to display
-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) Pickerview;
Returns the number of data rows in the component column
-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component;
@end
Two: Agent method:
* * Proxy protocol, all methods are optional */
@protocol uipickerviewdelegate<nsobject>
@optional
Returns the width of the specified column
-(CGFloat) Pickerview: (Uipickerview *) Pickerview widthforcomponent: (nsinteger) component;
Returns the row height of the specified column
-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component;
/**
The following three returns the contents of the corresponding row for the specified column
Can be:
1> NSString
2> nsattributedstring
If the above two methods are implemented at the same time, it is preferable to select the Nsattributedstring proxy method execution
3> UIView
If the view is not visible, the system caches the unused views and callbacks for reuse
If a different object is returned, the callback view is released
The custom view is displayed in the middle of the row
*/
-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component;
-(Nsattributedstring *) Pickerview: (Uipickerview *) Pickerview Attributedtitleforrow: (nsinteger) row forComponent: ( Nsinteger) component Ns_available_ios (6_0);
-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view;
Row row in the Component column is selected
-(void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (Nsinteger) component;
@end
Three: Common methods of Pickerview
Reload data for all columns
-(void) reloadallcomponents;
Reload data for the specified column
-(void) Reloadcomponent: (Nsinteger) component;
Selecting a row row in the specified component column does not trigger the proxy method
-(void) SelectRow: (Nsinteger) Row incomponent: (Nsinteger) component animated: (BOOL) animated; Scrolls the specified line to the middle position
Returns the currently selected row, or 1 if no row is selected
-(Nsinteger) Selectedrowincomponent: (Nsinteger) component;
Four: Ordering system code implementation:
#import "ViewController.h"@interfaceViewcontroller () <uipickerviewdatasource, uipickerviewdelegate>-(ibaction) Selectanyfood; @property (weak, nonatomic) Iboutlet Uipickerview*Pickview, @property (weak, nonatomic) Iboutlet UILabel*Fruitlab, @property (weak, nonatomic) Iboutlet UILabel*Foodlab, @property (weak, nonatomic) Iboutlet UILabel*Drinklab, @property (nonatomic, strong) Nsarray*Foods;@end@implementationViewcontroller#pragmaMark-Lazy Load data-(Nsarray *) foods{if(!_foods) {NSString*path = [[NSBundle mainbundle] Pathforresource:@"foods.plist"Oftype:nil]; Nsarray*foodsarr =[Nsarray Arraywithcontentsoffile:path]; _foods=Foodsarr; } return_foods;}- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib. //The view is loaded and the No. 0 row of data is displayed by default for(inti =0; i<self.foods.count; i++) {[Self Pickerview:nil didselectrow:0INCOMPONENT:I]; } }#pragmaMark-Data Source Method-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{returnSelf.foods.count;}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{//Note Why Self.foods[component] cannot point out count with dot syntax? /** [Self.foods objectatindex:0]; = = self.foods[0];//The effect of the two sentences is equivalent, and self calls objectatindex:0 this method, returns a universal pointer of type ID, Its true type is not detected until it is actually run, so it is not possible to use Self.foods[0].count directly. */ return[Self.foods[component] count];}#pragmaMark-Proxy Method-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (nsinteger) component{returnSelf.foods[component][row];}- (void) Pickerview: (Uipickerview *) Pickerview Didselectrow: (nsinteger) Row incomponent: (nsinteger) component{if(Component = =0) {Self.fruitLab.text=Self.foods[component][row]; } Else if(Component = =1) {Self.foodLab.text=Self.foods[component][row]; } Else{Self.drinkLab.text=Self.foods[component][row]; }}/** * Random order*/-(ibaction) Selectanyfood { for(inti =0; i<self.foods.count; i++) { //Remove the selected row intRow = (int) [Self.pickview selectedrowincomponent:i]; intRandom; Do{Random= Arc4random ()%[Self.foods[i] count]; } while(Row = =random); [Self.pickview selectrow:random incomponent:i Animated:yes]; [Self Pickerview:nil didselectrow:random incomponent:i]; }}@end
Effect:
V: Flag Display Code:
Data model
#import <Foundation/Foundation.h>@interface**icon; -(Instancetype) Initwithdic: (Nsdictionary *) dic; + (Instancetype) Flagwithdic: (Nsdictionary *) dic; + (Nsarray *) flagslist; @end
#import "LLFlag.h"@implementationLlflag-(Instancetype) Initwithdic: (Nsdictionary *) dic{if(self =[Super Init]) {[Self setvaluesforkeyswithdictionary:dic]; } returnSelf ;}+ (Instancetype) Flagwithdic: (Nsdictionary *) dic{return[Self alloc] initwithdic:dic];}+ (Nsarray *) flagslist{NSString*path = [[NSBundle mainbundle] Pathforresource:@"Flags"OfType:@"plist"]; Nsarray*dicarr =[Nsarray Arraywithcontentsoffile:path]; Nsmutablearray*tmparr =[[Nsmutablearray alloc] initWithCapacity:dicArr.count]; for(Nsdictionary *dicinchDicarr) {Llflag*flag =[Llflag Flagwithdic:dic]; [Tmparr Addobject:flag]; } returnTmparr;}@end
Xib Load View
#import <UIKit/UIKit.h>@class llflag; @interface *flag; + (instancetype) Flagview; @end
#import "LLFlagView.h"#import "LLFlag.h"@interfaceLlflagview () @property (weak, nonatomic) Iboutlet UILabel*Namelabel, @property (weak, nonatomic) Iboutlet Uiimageview*IconView;@end@implementationLlflagview+(instancetype) flagview{Llflagview*view = [[[NSBundle Mainbundle] loadnibnamed:@"Llflagview"Owner:nil Options:nil] lastobject]; returnview;}- (void) Setflag: (Llflag *) flag{_flag=Flag; Self.nameLabel.text=Flag.name; Self.iconView.image=[UIImage ImageNamed:flag.icon];}@end
Controller
#import "ViewController.h"#import "LLFlag.h"#import "LLFlagView.h"@interfaceViewcontroller () <uipickerviewdatasource, uipickerviewdelegate>@property (nonatomic, strong) Nsarray*flags; @property (weak, nonatomic) Iboutlet Uipickerview*Pickview;@end@implementationViewcontroller#pragmaMark-Lazy Loading-(Nsarray *) flags{if(!_flags) {_flags=[Llflag flagslist]; } return_flags;}- (void) viewdidload {[Super viewdidload]; //additional setup after loading the view, typically from a nib.}#pragmaMark-Data Source Method-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{return 1;}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{returnSelf.flags.count;}-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) Row forcomponent: (Nsinteger) component Reusingview: (UIView *) view{Llflagview*flagview =[Llflagview Flagview]; Flagview.flag=Self.flags[row]; returnFlagview;}-(CGFloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component{return -;}@end
Effect:
VI: Add:
Why [Self.foods[0] count] is used in the code implementation; Instead of directly using the point syntax self.foods[0].count to take a value.
[Self.foods objectatindex:0]; = = self.foods[0];//The effect of the two sentences is equivalent, and the self call objectatindex:0 this method, return is an ID type of the universal pointer, its real type to the actual run time can be detected, so can not directly use Self.foods [0].count.
UI Advanced Uipickerview---ordering system