#import <Foundation/Foundation.h>
@interface TXFlag : NSObject
@property (nonatomic, copy) NSString *icon;
@property (nonatomic, copy) NSString *name;
+ (instancetype)flagWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end
=========
#import "TXFlag.h"
@implementation TXFlag
+ (instancetype)flagWithDict:(NSDictionary *)dict
{
return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
if(self = [super init])
{
[self setValuesForKeysWithDictionary:dict];
}
return self;
}
@end
===============
#import <UIKit/UIKit.h>
@class TXFlag;
@interface TXFlageCell : UIView
@property (nonatomic, strong) TXFlag *flag;
+ (instancetype)flageCellWithReusingView:(UIView *)view;
+ (CGFloat)getRowHeight;
@end
=====================
#import "TXFlageCell.h"
#import "TXFlag.h"
@interface TXFlageCell()
@property (weak, nonatomic) IBOutlet UILabel *myname;
@property (weak, nonatomic) IBOutlet UIImageView *myImg;
@end
@implementation TXFlageCell
+ (instancetype)flageCellWithReusingView:(UIView *)view
{
if (view == nil) {
TXFlageCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"TXFlageCell" owner:nil options:nil] lastObject];
return cell;
} else {
return (TXFlageCell *)view;
}
}
- (void)setFlag:(TXFlag *)flag
{
_flag = flag;
self.myname.text = flag.name;
self.myImg.image = [UIImage imageNamed:flag.icon];
}
+ (CGFloat)getRowHeight
{
return 78;
}
@end
=================
#import "TXViewController.h"
#import "TXFlag.h"
#import "TXFlageCell.h"
@interface TXViewController () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, strong) NSArray *flags;
@end
@implementation TXViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
return self.flags.count;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
TXFlageCell *cell = [TXFlageCell flageCellWithReusingView:view];
cell.flag = self.flags[row];
return cell;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
return [TXFlageCell getRowHeight];
}
-(NSArray *)flags
{
if(_flags == nil)
{
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *flagArray = [NSMutableArray array];
for (NSDictionary *dict in dictArray) {
TXFlag *flag = [TXFlag flagWithDict:dict];
[flagArray addObject:flag];
}
_flags = flagArray;
}
return _flags;
}
@end
================