標籤:es2017 內容 let uil flags images UI undle string
QRViewController控制器
//// QRViewController.m//#import "QRViewController.h"#import "QRFlag.h"#import "QRFlagView.h"@interface QRViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>@property (nonatomic,strong) NSArray *flags;@end@implementation QRViewController- (void)viewDidLoad { [super viewDidLoad];}/** *懶載入國旗資料 */- (NSArray *)flags{ if(_flags==nil){ NSString *path=[[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"]; NSArray *arrayList=[NSArray arrayWithContentsOfFile:path]; NSMutableArray *dictArray=[NSMutableArray array]; for (NSDictionary *dict in arrayList) { QRFlag *flag=[QRFlag flagWithDict:dict]; [dictArray addObject:flag]; } _flags=dictArray; } return _flags;}#pragma mark - 設定資料來源/** *設定列數 */-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}/** *設定某一列的行 */- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return self.flags.count;}/** *設定某一列中的某一行的顯示資料 *///- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component//{// QRFlag *flag=self.flags[row];// return flag.name;//}/** * 第component列的第row行顯示怎樣的view * 每當有一行內容出現在視野範圍內,就會調用(調用頻率高) */-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ QRFlagView *flagView=[QRFlagView flagViewWithResuingView:view]; flagView.flag=self.flags[row]; return flagView;}- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{ return [QRFlagView flogViewHeight];}@end
QRFlag模型類
#import <Foundation/Foundation.h>@interface QRFlag : NSObject@property (nonatomic,copy) NSString *name;@property (nonatomic,copy) NSString *icon;+(instancetype)flagWithDict:(NSDictionary *)dict;- (instancetype)initWithDict:(NSDictionary *)dict;@end//===================#import "QRFlag.h"@implementation QRFlag+(instancetype)flagWithDict:(NSDictionary *)dict{ return [[self alloc] initWithDict:dict];}- (instancetype)initWithDict:(NSDictionary *)dict{ if(self=[super init]){ [self setValuesForKeysWithDictionary:dict]; } return self;}@end
XIB控制器
#import <UIKit/UIKit.h>@class QRFlag;@interface QRFlagView : UIView@property (nonatomic,strong) QRFlag *flag;+ (instancetype)flagViewWithResuingView:(UIView *)resuingView;+(CGFloat)flogViewHeight;@end//=================#import "QRFlagView.h"#import "QRFlag.h"@interface QRFlagView()@property (weak, nonatomic) IBOutlet UILabel *name;@property (weak, nonatomic) IBOutlet UIImageView *icon;@end@implementation QRFlagView+ (instancetype)flagViewWithResuingView:(UIView *)resuingView{ if(resuingView==nil) { NSBundle *bundle=[NSBundle mainBundle]; NSArray *obj=[bundle loadNibNamed:@"QRFlag" owner:nil options:nil]; return [obj lastObject]; }else{ return (QRFlag *)resuingView; }}+(CGFloat)flogViewHeight{ return 44;}-(void)setFlag:(QRFlag *)flag{ self.name.text=flag.name; self.icon.image=[UIImage imageNamed:flag.icon];}@end
ios 中pickerView用法之國旗選擇