[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 顾名思义也知 ...
随机推荐
- chrome下float元素下input选中内容bug
今天在写一个小demo的时候,发现chrome下一个很奇怪的bug. 我的代码如下: <!DOCTYPE html> <html lang="en"> &l ...
- [58 Argo]让argo跑起来
接上一章,使用命令mvn jetty:run启动Argo,进入localhost的页面: 58在这里给了几种常见的访问和传值方法的示例,当点击到第三条<区分queryString和form参数& ...
- POJ1037A decorative fence(好dp)
1037 带点组合的东西吧 黑书P257 其实我没看懂它写的嘛玩意儿 这题还是挺不错的 一个模糊的思路可能会好想一些 就是大体的递推方程 dp1[][]表示降序 dp2[][]表示升序 数组的含义为长 ...
- 宏buf_pool_t
typedef struct buf_pool_struct buf_pool_t; struct buf_pool_struct{ /** @name General fields */ /* @{ ...
- LeetCode Linked List Cycle 单链表环
题意:给一个单链表,判断其是否出现环! 思路:搞两个指针,每次,一个走两步,另一个走一步.若有环,他们会相遇,若无环,走两步的指针必定会先遇到NULL. /** * Definition for si ...
- java 错误:找不到或无法加载主类的解决办法
此类错误的常见解决办法: 1.是因为.java文件不在项目的src路径内,也就是说源代码未被eclipse编译,字节码不存在无法运行了在项目名上右键 -> Builder Path -> ...
- 《C++ Primer 4th》读书笔记 第10章-关联容器
原创文章,转载请注明出处:http://www.cnblogs.com/DayByDay/p/3936464.html
- 给table中某一列的文字右对齐
一般来说,没写过jquery的前端人员,肯定是定义一个class,给每一行的那列加上align_r{text-align:right}.这是很麻烦的. 所以用jquery来写,可以$("ta ...
- 云计算服务模型,第 3 部分: 软件即服务(PaaS)
英文原文:Cloud computing service models, Part 3: Software as a Service 软件即服务 (SaaS) 为商用软件提供基于网络的访问.您有可能已 ...
- Calling C++ code from C# z
http://blogs.msdn.com/b/borisj/archive/2006/09/28/769708.aspx I apologize for the long delay for thi ...