[iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
![](https://images0.cnblogs.com/blog/648473/201412/170049439372873.png)
![](https://images0.cnblogs.com/blog/648473/201412/170049452346602.png)
//
// 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
![](https://images0.cnblogs.com/blog/648473/201412/170049464225533.png)
#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
}
![](https://images0.cnblogs.com/blog/648473/201412/170049475159733.png)
![](https://images0.cnblogs.com/blog/648473/201412/170049489371734.png)
![](https://images0.cnblogs.com/blog/648473/201412/170049500624178.png)
//
// 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
![](https://images0.cnblogs.com/blog/648473/201412/170049512194865.png)
#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 顾名思义也知 ...
随机推荐
- 删除 GPT 保护分区
问题: 将内置和/或外置硬盘连接到 Windows XP 32 位操作系统时,将无法访问硬盘,“磁盘管理”将会报告该硬盘包含 GPT 保护分区.在此状态下,将无法对硬盘进行重新分区和格式化. 原因: ...
- 2014年百度之星程序设计大赛 - 资格赛 1004 Labyrinth(Dp)
题目链接 题目: Labyrinth Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- struts2中改变struts.xml默认路径
struts2.X配置文件默认存放路径在/WEB-INF/classes目录下,即将struts.xml放在src的目录下. 但是为了方便管理,开发人员把struts.xml放到其他位置,处理方法如下 ...
- UVa 11732 (Tire树) "strcmp()" Anyone?
这道题也是卡了挺久的. 给出一个字符串比较的算法,有n个字符串两两比较一次,问一共会有多少次比较. 因为节点会很多,所以Tire树采用了左儿子右兄弟的表示法来节省空间. 假设两个不相等的字符串的最长公 ...
- Java知识点:琐碎知识点(1)
Java基本介绍 SUN:Stanford University NetworkJava之父:James GoslingJava的跨平台性因为有Java虚拟机,运行class文件.Java吉祥物:Du ...
- Hide Xcode8 strange log.
Product > Scheme > Edit Scheme Environment Variables set OS_ACTIVITY_MODE = disable
- msssql 用numberic(38)替代int去解决int不够的问题
发现ms sql server的问题int(-2^31 ~ 2 ^ 31 -1)与程序中的UINT有时会对不上的时候, 发现如果用numberic(32)来做判断好像解决, 暂时就现在的插入操作来看, ...
- #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)宏的运行机理:1. ( (TYPE *)0 ) 将零转型为TY ...
- [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$$
- Android功能模块化之生成验证码Bitmap
Android生成验证码Bitmap,主要使用Canvas绘制,实现的步骤如下: 1.生成验证码.主要采用java的随机函数生成序号,然后对应获取预设的Char数组的值,生成长度固定的验证码: 2.C ...