数据模型:
- 在使用KVC模式时,如果字典中存储了图片名,可以直接声明UIImage属性,通过重写setter方法,直接赋值,
- 好处:在给子控件赋值时,统一使用点语法
#import <UIKit/UIKit.h>
@interface WQFlagData : NSObject
/**name*/
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) UIImage*icon;
+ (instancetype)flagWithDic:(NSDictionary *)dict;
@end
#import "WQFlagData.h"
@implementation WQFlagData
// 写程序一定要有扩展性
// instancetype: 自动识别当前是哪个类在调用,就会变成对应类的对象
// 为什么不用id,id 不能使用点语法
// id 可以调用任何对象的方法,坏处:不利于编译器=检查错误
// alloc 的调用用self,如果是子类,则代表创建的为子类对象
+ (instancetype)flagWithDic:(NSDictionary *)dict
{
WQFlagData *flagData = [[self alloc]init];
// 利用KVC字典转模型
[flagData setValuesForKeysWithDictionary:dict];
// [dict enumerateKeysAndObjectsUsingBlock:^(NSString *key, id obj, BOOL *stop) {
//
//
// NSString *funcName = [NSString stringWithFormat:@"set%@",key.capitalizedString];
//
// if ([flag respondsToSelector:@selector(funcName)]) {
//
// [flag setValue:obj forKeyPath:key];
//
// }
// }];
return flagData;
}
// 此处重写set方法,直接修改成UIImage类型,在空间赋值时,可以统一用点语法直接赋值,格式统一,
- (void)setIcon:(NSString *)icon
{
_icon = [UIImage imageNamed:icon];
}
// 遍历字典里面所有的key
// key:name
// 就去模型中查找有没有setName:,直接调用这个对象setName:赋值
// 假如没有找到setName:。就会去模型中查找有没有_name属性,_name = value
// 假如没有找到_name,还会去模型中查找name属性
// 最终没有找到,就会直接报错。
@end
- 自定义控件,通过加载xib方式(可以用button代替)
#import <UIKit/UIKit.h>
@class WQFlagData;
@interface WQFlagView : UIView
/** 数据模型*/
@property (nonatomic, strong) WQFlagData *flagData;
+ (instancetype)flagView;
@end
- 控制器
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
//
// ViewController.m
// PickerView国旗选择
//
// Created by mac on 15/6/9.
// Copyright (c) 2015年 mac. All rights reserved.
//
#import "ViewController.h"
#import "WQFlagData.h"
#import "WQFlagView.h"
@interface ViewController ()<UIPickerViewDelegate, UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *flagPickerView;
/**<#注释#>*/
@property (nonatomic, strong) NSMutableArray *flagData;
@end
@implementation ViewController
- (NSMutableArray *)flagData
{
if(_flagData == nil)
{
NSArray *arrFromPlist =[NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil]];
_flagData = [NSMutableArray array];
for (NSDictionary *dict in arrFromPlist)
{
WQFlagData *data = [WQFlagData flagWithDic:dict];
[_flagData addObject:data];
}
}
return _flagData;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.flagPickerView.dataSource = self;
self.flagPickerView.delegate = self;
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.flagData.count;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return 80;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
WQFlagView *flagView = [WQFlagView flagView];
flagView.flagData = self.flagData[row];
return flagView;
}
@end