A.需求
1.自定义一个UIView和xib,包含国家名和国旗显示
2.学习row的重用
B.实现步骤
1.准备plist文件和国旗图片
2.创建模型
//
// Flag.h
// CountriesSelection
//
// Created by hellovoidworld on 14/12/16.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <Foundation/Foundation.h> @interface Flag : NSObject /** 国家名 */
@property(nonatomic, copy) NSString *name; /** 国旗 */
@property(nonatomic, copy) NSString *icon; - (instancetype) initWithDictionary:(NSDictionary *) dictionary;
+ (instancetype) flagWithDictionary:(NSDictionary *) dictionary; @end
//
// Flag.m
// CountriesSelection
//
// Created by hellovoidworld on 14/12/16.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "Flag.h" @implementation Flag - (instancetype) initWithDictionary:(NSDictionary *) dictionary {
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dictionary];
} return self;
} + (instancetype) flagWithDictionary:(NSDictionary *) dictionary {
return [[self alloc] initWithDictionary:dictionary];
} @end
2.拖入PickerView, 准备主界面
3.实现dataSource和delegate方法
#pragma mark - dataSource function
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return ;
} - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.countries.count;
} #pragma mark - delegate function
// 使用返回UIView的row数据返回方法
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
//todo 创建自定义的view
}
4.创建自定义的UIView
5.设计自定row内容的xib,指定class
6.拖入控件到FlagView类,创建初始化方法和加载数据方法
//
// FlagView.h
// CountriesSelection
//
// Created by hellovoidworld on 14/12/16.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import <UIKit/UIKit.h> @class Flag;
@interface FlagView : UIView /** 国旗数据成员 */
@property(nonatomic, strong) Flag *flag; /** 自定义构造方法 */
+ (instancetype) flagViewWithPickerView:(UIPickerView *) pickerView; /** 定义自己的高度 */
+ (CGFloat) flagViewHeight; @end
//
// FlagView.m
// CountriesSelection
//
// Created by hellovoidworld on 14/12/16.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "FlagView.h"
#import "Flag.h" @interface FlagView() /** 国家名控件 */
@property (weak, nonatomic) IBOutlet UILabel *contryLabel;
/** 国旗控件 */
@property (weak, nonatomic) IBOutlet UIImageView *flagImage; @end @implementation FlagView + (instancetype) flagViewWithPickerView:(UIPickerView *) pickerView {
return [[NSBundle mainBundle] loadNibNamed:@"FlagView" owner:nil options:nil].lastObject;
} /** 加载数据 */
- (void) setFlag:(Flag *)flag {
_flag = flag; // 1.国家名
self.contryLabel.text = flag.name; // 2.国旗
self.flagImage.image = [UIImage imageNamed:flag.icon];
} /** 定义自己的高度 */
+ (CGFloat) flagViewHeight {
return ;
} @end
7.在控制器中加载解析数据
//
// ViewController.m
// CountriesSelection
//
// Created by hellovoidworld on 14/12/16.
// Copyright (c) 2014年 hellovoidworld. All rights reserved.
// #import "ViewController.h"
#import "Flag.h"
#import "FlagView.h" @interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate> /** 国家选择器 */
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView; /** 国家数组 */
@property(nonatomic, strong) NSArray *countries; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. // 设置dataSource
self.pickerView.dataSource = self;
// 设置delegate
self.pickerView.delegate = self;
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} /** 加载数据 */
- (NSArray *) countries {
if (nil == _countries) {
NSArray * dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil]]; NSMutableArray *mCountriesArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
Flag *flag = [Flag flagWithDictionary:dict];
[mCountriesArray addObject:flag];
} _countries = mCountriesArray;
} return _countries;
} #pragma mark - dataSource function
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return ;
} - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return self.countries.count;
} #pragma mark - delegate function
// 使用返回UIView的row数据返回方法
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
FlagView *flagView = [FlagView flagViewWithPickerView:self.pickerView];
flagView.flag = self.countries[row]; return flagView;
} /** 设置row的高度 */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
return [FlagView flagViewHeight];
} @end
out:
C.row的重用
PickerView没有像TableView一样提供缓存池的方法 dequeueReusableCellWithIdentifier
在PickerView的delegate的row数据加载方法中提供了reusingView,是iOS内核缓存的UIView
#pragma mark - delegate function
// 使用返回UIView的row数据返回方法
- (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { FlagView *flagView;
if (nil == view) {
flagView = [FlagView flagViewWithPickerView:self.pickerView];
} else {
NSLog(@"重用");
flagView = (FlagView *)view;
} flagView.flag = self.countries[row]; NSLog(@"flagView - %p, view - %p", flagView, view); return flagView;
}
==》但是在我的操作中,系统由始至终都没有传入缓存的UIView,每次都是使用了代码创建的FlagView
out:
2014-12-16 17:02:58.927 CountriesSelection[19352:1505008] flagView - 0x7ba7ee40, view - 0x0
2014-12-16 17:02:58.931 CountriesSelection[19352:1505008] flagView - 0x7b942300, view - 0x0
2014-12-16 17:02:58.936 CountriesSelection[19352:1505008] flagView - 0x79e44b80, view - 0x0
2014-12-16 17:02:58.937 CountriesSelection[19352:1505008] flagView - 0x79f64bc0, view - 0x0
2014-12-16 17:02:58.940 CountriesSelection[19352:1505008] flagView - 0x79f68e00, view - 0x0
2014-12-16 17:02:58.941 CountriesSelection[19352:1505008] flagView - 0x79f6a720, view - 0x0
2014-12-16 17:02:58.942 CountriesSelection[19352:1505008] flagView - 0x79f6bff0, view - 0x0
2014-12-16 17:02:58.943 CountriesSelection[19352:1505008] flagView - 0x79f6ff30, view - 0x0
2014-12-16 17:02:58.944 CountriesSelection[19352:1505008] flagView - 0x79f6e370, view - 0x0
2014-12-16 17:03:00.134 CountriesSelection[19352:1505008] flagView - 0x7b94a6c0, view - 0x0
2014-12-16 17:03:00.168 CountriesSelection[19352:1505008] flagView - 0x7b94b810, view - 0x0
2014-12-16 17:03:00.605 CountriesSelection[19352:1505008] flagView - 0x79f364c0, view - 0x0
2014-12-16 17:03:00.655 CountriesSelection[19352:1505008] flagView - 0x79f439e0, view - 0x0
2014-12-16 17:03:00.657 CountriesSelection[19352:1505008] flagView - 0x79f5a450, view - 0x0
2014-12-16 17:03:00.873 CountriesSelection[19352:1505008] flagView - 0x79f6cc60, view - 0x0
2014-12-16 17:02:58.931 CountriesSelection[19352:1505008] flagView - 0x7b942300, view - 0x0
2014-12-16 17:02:58.936 CountriesSelection[19352:1505008] flagView - 0x79e44b80, view - 0x0
2014-12-16 17:02:58.937 CountriesSelection[19352:1505008] flagView - 0x79f64bc0, view - 0x0
2014-12-16 17:02:58.940 CountriesSelection[19352:1505008] flagView - 0x79f68e00, view - 0x0
2014-12-16 17:02:58.941 CountriesSelection[19352:1505008] flagView - 0x79f6a720, view - 0x0
2014-12-16 17:02:58.942 CountriesSelection[19352:1505008] flagView - 0x79f6bff0, view - 0x0
2014-12-16 17:02:58.943 CountriesSelection[19352:1505008] flagView - 0x79f6ff30, view - 0x0
2014-12-16 17:02:58.944 CountriesSelection[19352:1505008] flagView - 0x79f6e370, view - 0x0
2014-12-16 17:03:00.134 CountriesSelection[19352:1505008] flagView - 0x7b94a6c0, view - 0x0
2014-12-16 17:03:00.168 CountriesSelection[19352:1505008] flagView - 0x7b94b810, view - 0x0
2014-12-16 17:03:00.605 CountriesSelection[19352:1505008] flagView - 0x79f364c0, view - 0x0
2014-12-16 17:03:00.655 CountriesSelection[19352:1505008] flagView - 0x79f439e0, view - 0x0
2014-12-16 17:03:00.657 CountriesSelection[19352:1505008] flagView - 0x79f5a450, view - 0x0
2014-12-16 17:03:00.873 CountriesSelection[19352:1505008] flagView - 0x79f6cc60, view - 0x0
2014-12-16 17:03:01.477 CountriesSelection[19352:1505008] flagView - 0x7b94ce90, view - 0x0