ios 中pickerView用法之国旗选择

Posted zzqqrr

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ios 中pickerView用法之国旗选择相关的知识,希望对你有一定的参考价值。

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用法之国旗选择的主要内容,如果未能解决你的问题,请参考以下文章

ios中 pickerView的用法

谈谈-Android-PickerView系列之源码解析

ios 中pickerView城市选择和UIDatePicker生日选择

iOS开发之基础视图—— UIPickerView

谈谈-Android-PickerView系列之介绍与使用

如何在 ios 中点击 UITextView 打开 Pickerview(dropdown)?