1. 程式人生 > >ios 中pickerView用法之國旗選擇

ios 中pickerView用法之國旗選擇

spa copy interface option sin source 出現 color import

技術分享

QRViewController控制器

//
//  QRViewController.m
//
#import "QRViewController.h"
#import "QRFlag.h"
#import "QRFlagView.h"

@interface QRViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (nonatomic,strong) NSArray *flags;
@end

@implementation QRViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
/** *懶加載國旗數據 */ - (NSArray *)flags { if(_flags==nil){ NSString *path=[[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"]; NSArray *arrayList=[NSArray arrayWithContentsOfFile:path]; NSMutableArray *dictArray=[NSMutableArray array]; for (NSDictionary *dict in
arrayList) { QRFlag *flag=[QRFlag flagWithDict:dict]; [dictArray addObject:flag]; } _flags=dictArray; } return _flags; } #pragma mark - 設置數據源 /** *設置列數 */ -(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } /** *設置某一列的行
*/ - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return self.flags.count; } /** *設置某一列中的某一行的顯示數據 */ //- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component //{ // QRFlag *flag=self.flags[row]; // return flag.name; //} /** * 第component列的第row行顯示怎樣的view * 每當有一行內容出現在視野範圍內,就會調用(調用頻率高) */ -(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { QRFlagView *flagView=[QRFlagView flagViewWithResuingView:view]; flagView.flag=self.flags[row]; return flagView; } - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component { return [QRFlagView flogViewHeight]; } @end

QRFlag模型類

#import <Foundation/Foundation.h>

@interface QRFlag : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) NSString *icon;
+(instancetype)flagWithDict:(NSDictionary *)dict;
- (instancetype)initWithDict:(NSDictionary *)dict;
@end


//===================

#import "QRFlag.h"

@implementation QRFlag
+(instancetype)flagWithDict:(NSDictionary *)dict
{
    return [[self alloc] initWithDict:dict];
}
- (instancetype)initWithDict:(NSDictionary *)dict
{
    if(self=[super init]){
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}
@end

XIB控制器

#import <UIKit/UIKit.h>

@class QRFlag;

@interface QRFlagView : UIView
@property (nonatomic,strong) QRFlag *flag;

+ (instancetype)flagViewWithResuingView:(UIView *)resuingView;
+(CGFloat)flogViewHeight;
@end



//=================


#import "QRFlagView.h"
#import "QRFlag.h"

@interface QRFlagView()
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *icon;
@end

@implementation QRFlagView

+ (instancetype)flagViewWithResuingView:(UIView *)resuingView
{
    if(resuingView==nil)
    {
    NSBundle *bundle=[NSBundle mainBundle];
    NSArray *obj=[bundle loadNibNamed:@"QRFlag" owner:nil options:nil];
    return [obj lastObject];
    }else{
        return (QRFlag *)resuingView;
    }
}
+(CGFloat)flogViewHeight
{
    return 44;
}
-(void)setFlag:(QRFlag *)flag
{
    self.name.text=flag.name;
    self.icon.image=[UIImage imageNamed:flag.icon];
}

@end

ios 中pickerView用法之國旗選擇