源码下载地址:http://download.csdn.net/detail/liu537192/8515559
效果图:
核心代码:
//
// JLViewController.m
// 02-国旗
//
// Created by XinYou on 15-3-9.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLViewController.h"
#import "JLFlag.h"
#import "JLFlagView.h"
@interface JLViewController ()
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (nonatomic, strong)NSArray *flags;
@end
@implementation JLViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// 设置数据源
self.pickerView.dataSource = self;
// 设置代理
self.pickerView.delegate = self;
}
- (NSArray *)flags{
if (_flags == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *tempArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
JLFlag *flag = [JLFlag flagWithDict:dict];
[tempArray addObject:flag];
}
_flags = tempArray;
}
return _flags;
}
#pragma mark -数据源方法
/**
* pickerView共有多少列
*/
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
/**
* pickerView每一列各有多少行
*/
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.flags.count;
}
/**
* pickerView每一行的高度
*/
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 44;
}
#pragma mark -代理方法
/**
* 第component列的第row行显示什么文字
*/
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//
//
//}
/**
* 第component列的第row行显示怎样的view
*/
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
JLFlagView *flagView = [JLFlagView flagViewWithReusingView:view];
flagView.flag = self.flags[row];
return flagView;
}
@end
//
// JLFlag.h
// 02-国旗
//
// Created by XinYou on 15-3-9.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@interface JLFlag : NSObject
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *icon;
- (instancetype)initWithDict:(NSDictionary *)dict;
+ (instancetype)flagWithDict:(NSDictionary *)dict;
@end
//
// JLFlag.m
// 02-国旗
//
// Created by XinYou on 15-3-9.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLFlag.h"
@implementation JLFlag
- (instancetype)initWithDict:(NSDictionary *)dict{
if (self = [super init]) {
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
+ (instancetype)flagWithDict:(NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
//
// JLFlagView.h
// 02-国旗
//
// Created by XinYou on 15-3-9.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import
@class JLFlag;
@interface JLFlagView : UIView
+ (instancetype)flagViewWithReusingView:(UIView *)resuingView;
@property (nonatomic, strong)JLFlag *flag;
@end
//
// JLFlagView.m
// 02-国旗
//
// Created by XinYou on 15-3-9.
// Copyright (c) 2015年 vxinyou. All rights reserved.
//
#import "JLFlagView.h"
#import "JLFlag.h"
@interface JLFlagView()
/**
* 显示国家名的label
*/
@property (weak, nonatomic) IBOutlet UILabel *countryNameLabel;
/**
* 显示国旗的iamgeView
*/
@property (weak, nonatomic) IBOutlet UIImageView *nationalFlagView;
@end
@implementation JLFlagView
+ (instancetype)flagViewWithReusingView:(UIView *)resuingView{
// 类似于UITableView中重复利用cell,提高效率
if (resuingView == nil) {
JLFlagView *flagView = [[[NSBundle mainBundle] loadNibNamed:@"JLFlagView" owner:nil options:nil] lastObject];
return flagView;
}
return (JLFlagView *)resuingView;
}
- (void)setFlag:(JLFlag *)flag{
_flag = flag;
self.countryNameLabel.text = self.flag.name;
self.nationalFlagView.image = [UIImage imageNamed:self.flag.icon];
}
@end