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