ios-day10-02(UIPickerView的使用。使用xib自定义pickerView每一行显示的view)

源码下载地址:http://download.csdn.net/detail/liu537192/8515559


效果图:



核心代码:

//
//  JLViewController.m
//  02-国旗
//
//  Created by XinYou on 15-3-9.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLViewController.h"
#import "JLFlag.h"
#import "JLFlagView.h"

@interface JLViewController ()
    
    
     
     
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;

@property (nonatomic, strong)NSArray *flags;

@end

@implementation JLViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置数据源
    self.pickerView.dataSource = self;
    // 设置代理
    self.pickerView.delegate = self;
    
}

- (NSArray *)flags{

    if (_flags == nil) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
        
        NSArray *dictArray = [NSArray arrayWithContentsOfFile:path];
        
        NSMutableArray *tempArray = [NSMutableArray array];
        
        for (NSDictionary *dict in dictArray) {
            JLFlag *flag = [JLFlag flagWithDict:dict];
            
            [tempArray addObject:flag];
        }
        _flags = tempArray;
    }
    
    return _flags;
}

#pragma mark -数据源方法
/**
 *  pickerView共有多少列
 */
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{

    return 1;
}
/**
 *  pickerView每一列各有多少行
 */
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{

    return self.flags.count;
}
/**
 *  pickerView每一行的高度
 */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{

    return 44;
}

#pragma mark -代理方法
/**
 *  第component列的第row行显示什么文字
 */
//- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//
//    
//}

/**
 *  第component列的第row行显示怎样的view
 */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{

    JLFlagView *flagView = [JLFlagView flagViewWithReusingView:view];
    flagView.flag = self.flags[row];
    
    return flagView;
}


@end

    
    
//
//  JLFlag.h
//  02-国旗
//
//  Created by XinYou on 15-3-9.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
    
    
     
     

@interface JLFlag : NSObject

@property (nonatomic, copy)NSString *name;

@property (nonatomic, copy)NSString *icon;

- (instancetype)initWithDict:(NSDictionary *)dict;

+ (instancetype)flagWithDict:(NSDictionary *)dict;

@end
//
//  JLFlag.m
//  02-国旗
//
//  Created by XinYou on 15-3-9.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLFlag.h"

@implementation JLFlag

- (instancetype)initWithDict:(NSDictionary *)dict{

    if (self = [super init]) {
        [self setValuesForKeysWithDictionary:dict];
    }
    
    return self;
}

+ (instancetype)flagWithDict:(NSDictionary *)dict{

    return [[self alloc] initWithDict:dict];
}

@end
//
//  JLFlagView.h
//  02-国旗
//
//  Created by XinYou on 15-3-9.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import 
     
     
      
      
@class JLFlag;

@interface JLFlagView : UIView

+ (instancetype)flagViewWithReusingView:(UIView *)resuingView;

@property (nonatomic, strong)JLFlag *flag;

@end
//
//  JLFlagView.m
//  02-国旗
//
//  Created by XinYou on 15-3-9.
//  Copyright (c) 2015年 vxinyou. All rights reserved.
//

#import "JLFlagView.h"
#import "JLFlag.h"

@interface JLFlagView()
/**
 *  显示国家名的label
 */
@property (weak, nonatomic) IBOutlet UILabel *countryNameLabel;
/**
 *  显示国旗的iamgeView
 */
@property (weak, nonatomic) IBOutlet UIImageView *nationalFlagView;

@end

@implementation JLFlagView

+ (instancetype)flagViewWithReusingView:(UIView *)resuingView{

    // 类似于UITableView中重复利用cell,提高效率
    if (resuingView == nil) {
        
        JLFlagView *flagView = [[[NSBundle mainBundle] loadNibNamed:@"JLFlagView" owner:nil options:nil] lastObject];
       
        return flagView;
    }
    
    return (JLFlagView *)resuingView;
}

- (void)setFlag:(JLFlag *)flag{

    _flag = flag;
    
    self.countryNameLabel.text = self.flag.name;
    
    self.nationalFlagView.image = [UIImage imageNamed:self.flag.icon];
}

@end

     
     
    
    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值