码迷,mamicode.com
首页 > 其他好文 > 详细

2016 - 1 - 3 国旗选择demo

时间:2016-01-03 23:58:05      阅读:325      评论:0      收藏:0      [点我收藏+]

标签:


//
// ViewController.m // 国旗 // // Created by Mac on 16/1/3. // Copyright © 2016年 Mac. All rights reserved. // #import "ViewController.h" #import "FlagView.h" #import "CZFlag.h" @interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate> @property (nonatomic, strong)NSArray *flags; @end @implementation ViewController - (NSArray *)flags { if (!_flags) { NSArray *array = [CZFlag flagList]; _flags = array; } return _flags; } - (void)viewDidLoad { [super viewDidLoad]; // NSLog(@"%@",self.flags); } #pragma mark - 数据源方法 - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView { return 1; } - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component { return 10; } #pragma mark - 代理方法 //设置 控件的内容方法 - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view { FlagView *flagView =(FlagView *)view; if (!flagView) { flagView = [FlagView flagView]; } #warning 一般设置自定义的view的大小的时候 不直接使用frame与bounds. flagView.bounds = CGRectMake(0, 0, 375, 50); flagView.flag = self.flags[row]; return flagView; } // @end

以上为ViewController中的代码

FlagView中的代码:

#import <UIKit/UIKit.h>
#import "CZFlag.h"
@interface FlagView : UIView
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@property (weak, nonatomic) IBOutlet UILabel *nameView;
@property (nonatomic,  strong) CZFlag *flag;
+ (instancetype )flagView;
@end



//.m中
#import "FlagView.h"
@implementation FlagView
+ (instancetype )flagView
{
    return [[[NSBundle mainBundle] loadNibNamed:@"FlagView" owner:nil options:nil] lastObject];
}
- (void)setFlag:(CZFlag *)flag
{
    self.nameView.text = flag.name;
    self.iconView.image = [UIImage imageNamed:flag.icon];
}
@end

CZFlag中

#import <Foundation/Foundation.h>

@interface CZFlag : NSObject
@property (nonatomic, copy)NSString *name;
@property (nonatomic, copy)NSString *icon;
+ (NSArray *)flagList;
- (instancetype)initWithDic:(NSDictionary *)dic;
+ (instancetype)flagWithDic:(NSDictionary *)dic;
@end
//.m中
#import "CZFlag.h"
@implementation CZFlag
- (instancetype)initWithDic:(NSDictionary *)dic
{
    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dic];
    }
     return self;
}
+ (instancetype)flagWithDic:(NSDictionary *)dic
{
  CZFlag *flag =  [[CZFlag alloc] initWithDic:dic];
    return flag;
}
+ (NSArray *)flagList
{
    NSString *path = [[NSBundle mainBundle] pathForResource:@"flags" ofType:@"plist"];
    NSMutableArray *tmpArray = [NSMutableArray array];
    NSArray *dicArray = [NSArray arrayWithContentsOfFile:path];
    for (NSDictionary *dic in dicArray) {
     CZFlag *flag =  [CZFlag flagWithDic:dic];
        [tmpArray addObject:flag];
    }
    return tmpArray;
}
@end

效果如下

技术分享

2016 - 1 - 3 国旗选择demo

标签:

原文地址:http://www.cnblogs.com/BJTUzhengli/p/5097422.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!