IOS第11天(2:UIPickerView自定义国旗选择)
国旗选择
#import "HMViewController.h"
#import "HMFlag.h"
#import "HMFlagView.h" @interface HMViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> @property(nonatomic,strong)NSArray *flags; @end @implementation HMViewController -(NSArray *)flags{
if (_flags == nil) {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"];
NSArray *flagsArray = [NSArray arrayWithContentsOfFile:filePath]; NSMutableArray *flagsM = [NSMutableArray array]; for (NSDictionary *dict in flagsArray) {
HMFlag *flag = [HMFlag flagWithDict:dict];
[flagsM addObject:flag];
} _flags = flagsM; } return _flags;
} - (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib. NSLog(@"%@",self.flags);
} #pragma mark -pickerView的数据源 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return ;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.flags.count;
} //-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
// HMFlag *flag =self.flags[row];
// return flag.name;
//} #pragma mark 通常用于自定pickerView的cellView
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
#warning 在ios7当中,view的可循环引用是用bug。 HMFlag *flag =self.flags[row];
// UILabel *label = [[UILabel alloc] init];
// //自定义view的时候,设置x,y无效,所以不要做无用式
// //label.frame = CGRectMake(10, 10, 200, 44);
// label.bounds = CGRectMake(0, 0, 200, 44);
// label.backgroundColor = [UIColor grayColor];
// label.text = flag.name;
HMFlagView *flagView = nil; //如果view不为空,代表有可循环使用的view
if (view != nil) {
flagView = (HMFlagView *)view;
}else{
flagView = [HMFlagView flagView];
} //设置flagview的数据
flagView.flag = flag; NSLog(@"%d %p",row,flagView); return flagView;
} #pragma mark 设置picker里的第每一个view的高度
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return ;
}
*****HMFlagView.m
#import "HMFlagView.h"
#import "HMFlag.h" @interface HMFlagView()
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UIImageView *imageView; @end @implementation HMFlagView - (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
} +(instancetype)flagView{
return [[[NSBundle mainBundle] loadNibNamed:@"HMFlagView" owner:nil options:nil] lastObject];
} /*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/ -(void)setFlag:(HMFlag *)flag{
_flag = flag;
self.nameLabel.text = flag.name;
self.imageView.image = [UIImage imageNamed:flag.icon]; } @end
*****HMFlagView.h
#import <UIKit/UIKit.h>
@class HMFlag;
@interface HMFlagView : UIView +(instancetype)flagView;
@property(nonatomic,strong)HMFlag *flag; @end
***模型HMFlag.m
#import "HMFlag.h"
@implementation HMFlag
-(instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+(instancetype)flagWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
***模型HMFlag.h
#import <Foundation/Foundation.h> @interface HMFlag : NSObject @property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *icon; -(instancetype)initWithDict:(NSDictionary *)dict;
+(instancetype)flagWithDict:(NSDictionary *)dict; @end
IOS第11天(2:UIPickerView自定义国旗选择)的更多相关文章
- IOS第11天(3:UIPickerView省市联动)
********* #import "ViewController.h" #import "Province.h" @interface ViewControl ...
- IOS第11天(1:UIPickerView点餐)
UIPickerView #import "ViewController.h" @interface ViewController ()<UIPickerViewDataSo ...
- iOS开发UI篇—CAlayer(自定义layer)
iOS开发UI篇—CAlayer(自定义layer) 一.第一种方式 1.简单说明 以前想要在view中画东西,需要自定义view,创建一个类与之关联,让这个类继承自UIView,然后重写它的Draw ...
- iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结
iOS回顾笔记(08) -- 自定义Cell的类型和创建步骤总结 项目中我们常见的自定义cell主要分为两种 等高cell:如应用列表.功能列表 非等高cell:如微博列表.QQ聊天页面 下面对这 ...
- iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局
iOS开发UI篇—使用xib自定义UItableviewcell实现一个简单的团购应用界面布局 一.项目文件结构和plist文件 二.实现效果 三.代码示例 1.没有使用配套的类,而是直接使用xib文 ...
- iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解
iOS回顾笔记(03) -- 自定义View的封装和xib文件的使用详解 iOS开发中,我们常常将一块View封装起来,以便于统一管理内部的子控件.如iOS回顾笔记(02)中的"书" ...
- iOS边练边学--UIPickerView和UIDatePicker的简单使用
一.点菜系统练习(UIPickerView) <1>UIPickerView的常用代理方法介绍 #pragma mark - <UIPickerViewDelegate> // ...
- IOS研究院之打开照相机与本地相册选择图片(六)
原创文章如需转载请注明:转载自雨松MOMO程序研究院本文链接地址:IOS研究院之打开照相机与本地相册选择图片(六) Hello 大家好 IOS的文章好久都木有更新了,今天更新一篇哈. 这篇文章主要学习 ...
- iOS探索:对NSArray中自定义的对象进行排序
http://mobile.51cto.com/hot-434804.htm 我们开发的每个程序都会使用到一些数据,而这些数据一般被封装在一个自定义的类中.例如一个音乐程序可能会有一个Song类,聊天 ...
随机推荐
- 把所有css放到一个css文件的格式
@charset "utf-8"; @import url("global.css"); @import url("common.css") ...
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- json时间格式化问题
function jsonDateFormat(jsonDate) {//json日期格式转换为正常格式 try { var date = new Date(parseInt(jsonDate.rep ...
- BZOJ4444 : [Scoi2015]国旗计划
首先将坐标离散化,因为区间互不包含,可以理解为对于每个起点输出最少需要多少个战士. 将环倍长,破环成链,设$f[i]$表示区间左端点不超过$i$时右端点的最大值,可以通过$O(n)$递推求出. 那么如 ...
- java语言特性概述
一.前言 我们都知道java是面向对象的编程,其中四个基本特性:抽象.封装.继承.多态.这四个特性,概括起来可以这么理解,抽象.封装.继承是多态的基础,多态是抽象.封装.继承的表现. 二. JAVA ...
- 偶然的发现(与Code无关)
最近做后台用户注册, 在考虑不使用验证码, 百度搜了一下看了看一些相关技术, 发现了个小说——[万恶的验证码], 看了挺搞笑分享一下:原文链接 万恶的验证码 前言: 传说中,它是最为邪恶的吸血鬼,它是 ...
- 【BZOJ】3240: [Noi2013]矩阵游戏
题意 给出\(n, m(1 \le n, m \le 10^{1000000})\),求\(f(n, m) \ \mod \ 10^9+7\) $$\begin{cases}f(1, 1) = 1 \ ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ...
- Android --日期控件使用并显示选择的日期
1. main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns: ...
- LINUX 下通过lsof恢复被误删除的文件
当Linux计算机受到入侵时,常见的情况是日志文件被删除,以掩盖攻击者的踪迹.管理错误也可能导致意外删除重要的文件,比如在清理旧日志时,意外地删除了数据库的活动事务日志.有时可以通过lsof来恢复这些 ...