iOS 11th day (2:uipickerview custom flag selection)

Source: Internet
Author: User

Flag selection

#import "HMViewController.h"#import "HMFlag.h"#import "HMFlagView.h"@interfaceHmviewcontroller () <UIPickerViewDataSource,UIPickerViewDelegate>@property (nonatomic,strong) Nsarray*flags;@end@implementationHmviewcontroller-(Nsarray *) flags{if(_flags = =Nil) {NSString*filepath = [[NSBundle mainbundle] Pathforresource:@"Flags"OfType:@"plist"]; Nsarray*flagsarray =[Nsarray Arraywithcontentsoffile:filepath]; Nsmutablearray*FLAGSM =[Nsmutablearray array];  for(Nsdictionary *dictinchFlagsarray) {Hmflag*flag =[Hmflag flagwithdict:dict];        [Flagsm Addobject:flag]; } _flags=flagsm; }        return_flags;}- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib.NSLog (@"%@", self.flags);}#pragmaMark-pickerview Data Source-(Nsinteger) Numberofcomponentsinpickerview: (Uipickerview *) pickerview{return 1;}-(Nsinteger) Pickerview: (Uipickerview *) Pickerview numberofrowsincomponent: (nsinteger) component{returnSelf.flags.count;}//-(NSString *) Pickerview: (Uipickerview *) Pickerview Titleforrow: (nsinteger) Row forcomponent: (Nsinteger) component{//Hmflag *flag =self.flags[row];//return flag.name;//}#pragmaMark is typically used to customize Pickerview's cellview-(UIView *) Pickerview: (Uipickerview *) Pickerview Viewforrow: (nsinteger) row Forcomponent: (Nsinteger) component Reusingview: (UIView *) view{#warningIn iOS7, a circular reference to a view is a bug.Hmflag*flag =Self.flags[row];//UILabel *label = [[UILabel alloc] init];//    //when customizing the view, setting x, Y is not valid, so don't do useless//    //label.frame = CGRectMake (ten, ten , A.);//label.bounds = CGRectMake (0, 0, ();//Label.backgroundcolor = [Uicolor graycolor];//label.text = flag.name;Hmflagview *flagview =Nil; //If view is not empty, it represents a view that can be recycled    if(View! =Nil) {Flagview= (Hmflagview *) view; }Else{Flagview=[Hmflagview Flagview]; }        //set the Flagview dataFlagview.flag =Flag; NSLog (@"%d%p", Row,flagview); returnFlagview;}#pragmaMark sets the height of each view in Picker-(cgfloat) Pickerview: (Uipickerview *) Pickerview rowheightforcomponent: (nsinteger) component{return  -;}

hmflagview.m

#import "HMFlagView.h"#import "HMFlag.h"@interfaceHmflagview () @property (weak, nonatomic) Iboutlet UILabel*Namelabel, @property (weak, nonatomic) Iboutlet Uiimageview*ImageView;@end@implementationHmflagview- (ID) initWithFrame: (cgrect) frame{ Self=[Super Initwithframe:frame]; if(self) {//Initialization Code    }    returnSelf ;}+(instancetype) flagview{return[[[NSBundle Mainbundle] loadnibnamed:@"Hmflagview"Owner:nil Options:nil] lastobject];}/*//Only override drawrect:if your perform custom drawing.//an empty implementation adversely affects performance Duri ng animation.-(void) DrawRect: (cgrect) rect{//Drawing code}*/-(void) Setflag: (Hmflag *) flag{_flag=Flag; Self.nameLabel.text=Flag.name; Self.imageView.image=[UIImage ImageNamed:flag.icon]; }@end

HMFlagView.h

#import <UIKit/UIKit.h>@class  hmflag; @interface Hmflagview:uiview+*flag; @end

Model HMFLAG.M

  #import  "     @implementation   Hmflag -(Instancetype) Initwithdict: (nsdictionary *) dict{ if  (self = [super Init]) {[Self setvaluesforkeyswithdictionary:dict];  return   self;}  + (Instancetype) flagwithdict: (nsdictionary *) dict{ return   [[self alloc] initwithdict:dict];}   @end  

Model HMFlag.h

#import <Foundation/Foundation.h>@interface**icon; -(Instancetype) Initwithdict: (Nsdictionary *) dict; + (Instancetype) flagwithdict: (Nsdictionary *) dict; @end

iOS 11th day (2:uipickerview custom flag selection)

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.