国旗选择

#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
05-04 03:32