ios 中pickerView用法之國旗選擇

來源:互聯網
上載者:User

標籤:es2017   內容   let   uil   flags   images   UI   undle   string   

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用法之國旗選擇

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.