[iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo


//
// 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

#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
}



//
// 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
//
// 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

#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;
}
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
[iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo的更多相关文章
- [iOS基础控件 - 6.10.1] PickerView 餐点搭配Demo
A.需求 1.使用PickerView做出有3列餐点(水果.主菜.饮料)的搭配Demo 2.选择的餐点实时显示在“显示区” 3.提供“随机”按钮,随机选择菜品搭配 B.实现步骤 1.拖入一个Pic ...
- [iOS基础控件 - 6.10.7] UIWindow
A.UIWindow概念 1.继承UIView,是一种特殊的UIView 2.通常一个APP只有一个UIWindow 3.iOS程序启动后,创建的第一个视图就是UIWindow 4.没有UIWindo ...
- [iOS基础控件 - 6.10.5] UIApplication
A.概念 1.UIApplication对象是应用程序的象征,每个应用都有 2.单例 3.[UIApplication sharedApplication] 获取 4.iOS启动创建的第一个对象 5. ...
- [iOS基础控件 - 6.10] Notification 通知机制
A.定义 iOS程序都有一个NSNotificationCenter的单例对象,用来负责发布不同对象之间的通知 任何对象都能够在NSNotificationCenter发布通知,发 ...
- [iOS基础控件 - 6.10.6] UIApplicationDelegate & 程序启动过程
A.概念 1.移动app非常容易受到其他的系统.软件事件的干扰,如来电.锁屏 2.app受到干扰的时候,UIApplication会通知delegate,来代理处理干扰事件 3.delegate可以处 ...
- [iOS基础控件 - 6.10.4] 项目启动原理 项目中的文件
A.项目中的常见文件 1.单元测试Test 2.Frameworks(xCode6 创建的SingleView Project没有) 依赖框架 3.Products 打包好的文件 4. p ...
- [iOS基础控件 - 6.10.3] DatePicker & UIToolBar
A.需求 1. 学习DatePicker的基本配置 2.使用TextField召唤指定类型的输入键盘View,这里使用DatePicker 3.给输入键盘上方加上一个UIToolBar,实现如关闭键盘 ...
- [iOS基础控件 - 5.5] 代理设计模式 (基于”APP列表"练习)
A.概述 在"[iOS基础控件 - 4.4] APP列表 进一步封装,初见MVC模式”上进一步改进,给“下载”按钮加上效果.功能 1.按钮点击后,显示为“已下载”,并且不 ...
- iOS 基础控件(下)
上篇介绍了UIButton.UILabel.UIImageView和UITextField,这篇就简短一点介绍UIScrollView和UIAlertView. UIScrollView 顾名思义也知 ...
随机推荐
- bzoj1064
很巧妙的题 首先有几种情况 1. 有环 2.两点间有多条路径 3.其他 3.显然最简单,最小是3,最大是每个弱联通块中最长链 2.显然,两点间两条路径的差是答案的倍数 1.出现环,那答案一定是其约数, ...
- parentNode(返回指定节点的父节点。)
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- BZOJ3858: Number Transformation
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=3858 题解:设第i个数为i*a;第i+1个数为(i+1)*b.则(i+1)*b>i*a; ...
- Cocoa Touch(一)开发基础:Xcode概念、目录结构、设计模式、代码风格
Xcode相关概念: 概念:project 指一个项目,该项目会负责管理软件产品的全部源代码文件.全部资源文件.相关配置,一个Project可以包含多个Target. 概念:target 一个targ ...
- dbms_stats.gather_table_stats与analyze table 的区别[转贴]
Analyze StatementThe ANALYZE statement can be used to gather statistics for a specific table, index ...
- Android 调用系统的邮箱app发送邮件
第一种: String[] email = { "3802**92@qq.com" }; // 需要注意,email必须以数组形式传入 Intent intent = new In ...
- 【转】移动web资源整理
目录(更新于20150311) meta基础知识 H5页面窗口自动调整到设备宽度,并禁止用户缩放页面 忽略将页面中的数字识别为电话号码 忽略Android平台中对邮箱地址的识别 当网站添加到主屏幕快速 ...
- 关于ThinkPHP中Session不能夸模块/控制器使用的问题-网上的答案我做个补充
1,确保c:/windows目录下有php.ini文件2,修改php.ini中的session.auto_start = 0 为 session.auto_start = 1 //设置自动开启ses ...
- Filezilla 多目录的访问设置
[转:http://hi.baidu.com/yangjie312/blog/item/125a8405423d896d03088171.html] 设置好主目录之后,点击“Add”按钮可以把其余的目 ...
- [OFBiz]开发 二
1.svn中check出的apache-ofbiz-10.04(svn_2010-04-01代码备分)由于它的所有文件都不含有中文,所以Eclipse使用什么编码方式都可以(ISO, GBK, UTF ...