博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
2016 - 1 - 3 国旗选择demo
阅读量:4656 次
发布时间:2019-06-09

本文共 2761 字,大约阅读时间需要 9 分钟。

////  ViewController.m//  国旗////  Created by Mac on 16/1/3.//  Copyright © 2016年 Mac. All rights reserved.//#import "ViewController.h"#import "FlagView.h"#import "CZFlag.h"@interface ViewController ()
@property (nonatomic, strong)NSArray *flags;@end@implementation ViewController- (NSArray *)flags{ if (!_flags) { NSArray *array = [CZFlag flagList]; _flags = array; } return _flags;}- (void)viewDidLoad { [super viewDidLoad];// NSLog(@"%@",self.flags);}#pragma mark - 数据源方法- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{ return 1;}- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{ return 10;}#pragma mark - 代理方法//设置 控件的内容方法- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{ FlagView *flagView =(FlagView *)view; if (!flagView) { flagView = [FlagView flagView]; } #warning 一般设置自定义的view的大小的时候 不直接使用frame与bounds. flagView.bounds = CGRectMake(0, 0, 375, 50); flagView.flag = self.flags[row]; return flagView;}//@end

以上为ViewController中的代码

FlagView中的代码:

#import 
#import "CZFlag.h"@interface FlagView : UIView@property (weak, nonatomic) IBOutlet UIImageView *iconView;@property (weak, nonatomic) IBOutlet UILabel *nameView;@property (nonatomic, strong) CZFlag *flag;+ (instancetype )flagView;@end//.m中#import "FlagView.h"@implementation FlagView+ (instancetype )flagView{ return [[[NSBundle mainBundle] loadNibNamed:@"FlagView" owner:nil options:nil] lastObject];}- (void)setFlag:(CZFlag *)flag{ self.nameView.text = flag.name; self.iconView.image = [UIImage imageNamed:flag.icon];}@end

CZFlag中

#import 
@interface CZFlag : NSObject@property (nonatomic, copy)NSString *name;@property (nonatomic, copy)NSString *icon;+ (NSArray *)flagList;- (instancetype)initWithDic:(NSDictionary *)dic;+ (instancetype)flagWithDic:(NSDictionary *)dic;@end//.m中#import "CZFlag.h"@implementation CZFlag- (instancetype)initWithDic:(NSDictionary *)dic{ if (self = [super init]) { [self setValuesForKeysWithDictionary:dic]; } return self;}+ (instancetype)flagWithDic:(NSDictionary *)dic{ CZFlag *flag = [[CZFlag alloc] initWithDic:dic]; return flag;}+ (NSArray *)flagList{ NSString *path = [[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"]; NSMutableArray *tmpArray = [NSMutableArray array]; NSArray *dicArray = [NSArray arrayWithContentsOfFile:path]; for (NSDictionary *dic in dicArray) { CZFlag *flag = [CZFlag flagWithDic:dic]; [tmpArray addObject:flag]; } return tmpArray;}@end

效果如下

转载于:https://www.cnblogs.com/BJTUzhengli/p/5097422.html

你可能感兴趣的文章
Python18天训练营第二课<基础1>
查看>>
seanborn使用函数regplot回归分析绘图
查看>>
第十篇----------javascript函数的三种定义方式及区别
查看>>
181. Employees Earning More Than Their Managers【leetcode】,sql,inner join ,where
查看>>
html 元素
查看>>
jQuery-qrcode.js 生成带Logo 的二维码
查看>>
2,[VS入门教程] 使用Visual Studio写c语言 入门与技巧精品文~~~~优化篇
查看>>
Objective-C 基础(五)
查看>>
【bzoj1705】[Usaco2007 Nov]Telephone Wire 架设电话线 dp
查看>>
使用 .NET 平台,如何玩转 Universal Windows 应用?
查看>>
ffmpeg的基本命令
查看>>
flex4+fms3.5+cs4开发实时音视频直播及点播详解
查看>>
打开VS2015提示“重新启动处于挂起状态。请在启动Visual Studio”之前重新启动
查看>>
form 中Enctype=multipart/form-data 的作用
查看>>
洛谷P1257 平面上的最接近点对
查看>>
BZOJ1657 [Usaco2006 Mar]Mooo 奶牛的歌声
查看>>
CSS/JavaScript hacks,browserhacks使用
查看>>
SQLServer内部数据库版本获取及匹配
查看>>
【转】<meta>标签用法
查看>>
ajax请求post和get的区别以及get post的选择
查看>>