标签:style blog http ar io color os 使用 sp
1 // 2 // Flag.h 3 // CountriesSelection 4 // 5 // Created by hellovoidworld on 14/12/16. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import <Foundation/Foundation.h> 10 11 @interface Flag : NSObject 12 13 /** 国家名 */ 14 @property(nonatomic, copy) NSString *name; 15 16 /** 国旗 */ 17 @property(nonatomic, copy) NSString *icon; 18 19 - (instancetype) initWithDictionary:(NSDictionary *) dictionary; 20 + (instancetype) flagWithDictionary:(NSDictionary *) dictionary; 21 22 @end
1 // 2 // Flag.m 3 // CountriesSelection 4 // 5 // Created by hellovoidworld on 14/12/16. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "Flag.h" 10 11 @implementation Flag 12 13 - (instancetype) initWithDictionary:(NSDictionary *) dictionary { 14 if (self = [super init]) { 15 [self setValuesForKeysWithDictionary:dictionary]; 16 } 17 18 return self; 19 } 20 21 + (instancetype) flagWithDictionary:(NSDictionary *) dictionary { 22 return [[self alloc] initWithDictionary:dictionary]; 23 } 24 25 @end
1 #pragma mark - dataSource function 2 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 3 return 1; 4 } 5 6 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 7 return self.countries.count; 8 } 9 10 #pragma mark - delegate function 11 // 使用返回UIView的row数据返回方法 12 - (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 13 //todo 创建自定义的view 14 }
1 // 2 // FlagView.h 3 // CountriesSelection 4 // 5 // Created by hellovoidworld on 14/12/16. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import <UIKit/UIKit.h> 10 11 @class Flag; 12 @interface FlagView : UIView 13 14 /** 国旗数据成员 */ 15 @property(nonatomic, strong) Flag *flag; 16 17 /** 自定义构造方法 */ 18 + (instancetype) flagViewWithPickerView:(UIPickerView *) pickerView; 19 20 /** 定义自己的高度 */ 21 + (CGFloat) flagViewHeight; 22 23 @end
1 // 2 // FlagView.m 3 // CountriesSelection 4 // 5 // Created by hellovoidworld on 14/12/16. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "FlagView.h" 10 #import "Flag.h" 11 12 @interface FlagView() 13 14 /** 国家名控件 */ 15 @property (weak, nonatomic) IBOutlet UILabel *contryLabel; 16 /** 国旗控件 */ 17 @property (weak, nonatomic) IBOutlet UIImageView *flagImage; 18 19 @end 20 21 @implementation FlagView 22 23 + (instancetype) flagViewWithPickerView:(UIPickerView *) pickerView { 24 return [[NSBundle mainBundle] loadNibNamed:@"FlagView" owner:nil options:nil].lastObject; 25 } 26 27 /** 加载数据 */ 28 - (void) setFlag:(Flag *)flag { 29 _flag = flag; 30 31 // 1.国家名 32 self.contryLabel.text = flag.name; 33 34 // 2.国旗 35 self.flagImage.image = [UIImage imageNamed:flag.icon]; 36 } 37 38 /** 定义自己的高度 */ 39 + (CGFloat) flagViewHeight { 40 return 44; 41 } 42 43 @end
1 // 2 // ViewController.m 3 // CountriesSelection 4 // 5 // Created by hellovoidworld on 14/12/16. 6 // Copyright (c) 2014年 hellovoidworld. All rights reserved. 7 // 8 9 #import "ViewController.h" 10 #import "Flag.h" 11 #import "FlagView.h" 12 13 @interface ViewController () <UIPickerViewDataSource, UIPickerViewDelegate> 14 15 /** 国家选择器 */ 16 @property (weak, nonatomic) IBOutlet UIPickerView *pickerView; 17 18 /** 国家数组 */ 19 @property(nonatomic, strong) NSArray *countries; 20 21 22 @end 23 24 @implementation ViewController 25 26 - (void)viewDidLoad { 27 [super viewDidLoad]; 28 // Do any additional setup after loading the view, typically from a nib. 29 30 // 设置dataSource 31 self.pickerView.dataSource = self; 32 // 设置delegate 33 self.pickerView.delegate = self; 34 } 35 36 - (void)didReceiveMemoryWarning { 37 [super didReceiveMemoryWarning]; 38 // Dispose of any resources that can be recreated. 39 } 40 41 /** 加载数据 */ 42 - (NSArray *) countries { 43 if (nil == _countries) { 44 NSArray * dictArray = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil]]; 45 46 NSMutableArray *mCountriesArray = [NSMutableArray array]; 47 for (NSDictionary *dict in dictArray) { 48 Flag *flag = [Flag flagWithDictionary:dict]; 49 [mCountriesArray addObject:flag]; 50 } 51 52 _countries = mCountriesArray; 53 } 54 55 return _countries; 56 } 57 58 #pragma mark - dataSource function 59 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { 60 return 1; 61 } 62 63 - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { 64 return self.countries.count; 65 } 66 67 #pragma mark - delegate function 68 // 使用返回UIView的row数据返回方法 69 - (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 70 FlagView *flagView = [FlagView flagViewWithPickerView:self.pickerView]; 71 flagView.flag = self.countries[row]; 72 73 return flagView; 74 } 75 76 /** 设置row的高度 */ 77 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { 78 return [FlagView flagViewHeight]; 79 } 80 81 @end
1 #pragma mark - delegate function 2 // 使用返回UIView的row数据返回方法 3 - (UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { 4 5 FlagView *flagView; 6 if (nil == view) { 7 flagView = [FlagView flagViewWithPickerView:self.pickerView]; 8 } else { 9 NSLog(@"重用"); 10 flagView = (FlagView *)view; 11 } 12 13 flagView.flag = self.countries[row]; 14 15 NSLog(@"flagView - %p, view - %p", flagView, view); 16 17 return flagView; 18 }
[iOS基础控件 - 6.10.2] PickerView 自定义row内容 国家选择Demo
标签:style blog http ar io color os 使用 sp
原文地址:http://www.cnblogs.com/hellovoidworld/p/4168454.html