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:03:01.477 CountriesSelection[19352:1505008] flagView - 0x7b94ce90, view - 0x0
 
 
 

[iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo的更多相关文章

  1. [iOS基础控件 - 6.10.1] PickerView 餐点搭配Demo

    A.需求 1.使用PickerView做出有3列餐点(水果.主菜.饮料)的搭配Demo 2.选择的餐点实时显示在“显示区” 3.提供“随机”按钮,随机选择菜品搭配   B.实现步骤 1.拖入一个Pic ...

  2. [iOS基础控件 - 6.10.7] UIWindow

    A.UIWindow概念 1.继承UIView,是一种特殊的UIView 2.通常一个APP只有一个UIWindow 3.iOS程序启动后,创建的第一个视图就是UIWindow 4.没有UIWindo ...

  3. [iOS基础控件 - 6.10.5] UIApplication

    A.概念 1.UIApplication对象是应用程序的象征,每个应用都有 2.单例 3.[UIApplication sharedApplication] 获取 4.iOS启动创建的第一个对象 5. ...

  4. [iOS基础控件 - 6.10] Notification 通知机制

    A.定义      iOS程序都有一个NSNotificationCenter的单例对象,用来负责发布不同对象之间的通知      任何对象都能够在NSNotificationCenter发布通知,发 ...

  5. [iOS基础控件 - 6.10.6] UIApplicationDelegate & 程序启动过程

    A.概念 1.移动app非常容易受到其他的系统.软件事件的干扰,如来电.锁屏 2.app受到干扰的时候,UIApplication会通知delegate,来代理处理干扰事件 3.delegate可以处 ...

  6. [iOS基础控件 - 6.10.4] 项目启动原理 项目中的文件

    A.项目中的常见文件 1.单元测试Test   2.Frameworks(xCode6 创建的SingleView Project没有) 依赖框架   3.Products 打包好的文件   4. p ...

  7. [iOS基础控件 - 6.10.3] DatePicker & UIToolBar

    A.需求 1. 学习DatePicker的基本配置 2.使用TextField召唤指定类型的输入键盘View,这里使用DatePicker 3.给输入键盘上方加上一个UIToolBar,实现如关闭键盘 ...

  8. [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)

    A.概述      在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能      1.按钮点击后,显示为“已下载”,并且不 ...

  9. iOS 基础控件(下)

    上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...

随机推荐

  1. 删除 GPT 保护分区

    问题: 将内置和/或外置硬盘连接到 Windows XP 32 位操作系统时,将无法访问硬盘,“磁盘管理”将会报告该硬盘包含 GPT 保护分区.在此状态下,将无法对硬盘进行重新分区和格式化. 原因: ...

  2. 2014年百度之星程序设计大赛 - 资格赛 1004 Labyrinth(Dp)

    题目链接 题目: Labyrinth Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  3. struts2中改变struts.xml默认路径

    struts2.X配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下. 但是为了方便管理,开发人员把struts.xml放到其他位置,处理方法如下 ...

  4. UVa 11732 (Tire树) "strcmp()" Anyone?

    这道题也是卡了挺久的. 给出一个字符串比较的算法,有n个字符串两两比较一次,问一共会有多少次比较. 因为节点会很多,所以Tire树采用了左儿子右兄弟的表示法来节省空间. 假设两个不相等的字符串的最长公 ...

  5. Java知识点:琐碎知识点(1)

    Java基本介绍 SUN:Stanford University NetworkJava之父:James GoslingJava的跨平台性因为有Java虚拟机,运行class文件.Java吉祥物:Du ...

  6. Hide Xcode8 strange log.

    Product > Scheme > Edit Scheme Environment Variables set OS_ACTIVITY_MODE = disable

  7. msssql 用numberic(38)替代int去解决int不够的问题

    发现ms sql server的问题int(-2^31 ~ 2 ^ 31 -1)与程序中的UINT有时会对不上的时候, 发现如果用numberic(32)来做判断好像解决, 暂时就现在的插入操作来看, ...

  8. #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

    #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...

  9. [Everyday Mathematics]20150110

    试证: $$\bex \vlm{n}\frac{\ln^2n}{n}\sum_{k=2}^{n-2}\frac{1}{\ln k\cdot \ln(n-k)}=1. \eex$$

  10. Android功能模块化之生成验证码Bitmap

    Android生成验证码Bitmap,主要使用Canvas绘制,实现的步骤如下: 1.生成验证码.主要采用java的随机函数生成序号,然后对应获取预设的Char数组的值,生成长度固定的验证码: 2.C ...