这个小实例主要涉及UIPickView及其代理UIPickViewDataSource和UIPickViewDelegate
极客学院视频地址:http://www.jikexueyuan.com/course/384.html
源码地址:https://github.com/ianzhengnan/Country
实例效果:
实现步骤如下:
1 创建项目,导入图片和plist文件
2 创建模型类。
#import <Foundation/Foundation.h>
@interface CountryInfo : NSObject
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *icon;
- (instancetype)initWithDict: (NSDictionary *)dict;
+ (instancetype)countryWithDict: (NSDictionary *)dict;
@end
//
// CountryInfo.m
// Country
//
// Created by on 15/7/1.
// Copyright (c) 2015年 zhengnan. All rights reserved.
//
#import "CountryInfo.h"
@implementation CountryInfo
- (instancetype)initWithDict: (NSDictionary *)dict{
self = [super init];
if (self) {
[self setValuesForKeysWithDictionary:dict]; //通常实际项目中不用kvc的方式,通常都是读取的json xml的方式ß
}
return self;
}
+ (instancetype)countryWithDict: (NSDictionary *)dict{
return [[self alloc] initWithDict:dict];
}
@end
3 懒加载导入plist文件数据
//lazy load
- (NSArray *)dataArray{
if (!_dataArray) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"flags.plist" ofType:nil];
NSArray *array = [NSArray arrayWithContentsOfFile:path];
NSMutableArray *muArr = [NSMutableArray arrayWithCapacity:array.count];
for (NSDictionary *dict in array) {
CountryInfo *countryModel = [CountryInfo countryWithDict:dict];
[muArr addObject:countryModel];
}
_dataArray = [muArr copy];
}
return _dataArray;
}
3.1 漏了一步,在main.storyboard文件中,拖入一个UIPickView.在上面点击右键通过拖动设置它的DataSource和Delegate为ViewController
并在ViewController.m文件头部加入< UIPickViewDataSource,
类文件和一个xib文件,需要他们同名
5 在xib文件中,加入一个view, 并拖入一个lable和一个UIImageView
调整他们的大小
6. view类
在其中拖入xib文件中的控件,并创建一个模型的实例。并重写模型的setter方法,把它绑定到xib文件的控件上。
//
// CountryFlagView.h
// Country
//
// Created by on 15/7/2.
// Copyright (c) 2015年 zhengnan. All rights reserved.
//
#import <UIKit/UIKit.h>
@class CountryInfo; //为了防止循环导入,所以这里用class
@interface CountryFlagView : UIView
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UIImageView *flag;
@property (nonatomic, strong) CountryInfo *countryModel;
+ (instancetype)countryView;
+ (CGFloat)rowHeight;
@end
//
// CountryFlagView.m
// Country
//
// Created by on 15/7/2.
// Copyright (c) 2015年 zhengnan. All rights reserved.
//
#import "CountryFlagView.h"
#import "CountryInfo.h"
@implementation CountryFlagView
+ (instancetype)countryView{
return [[[NSBundle mainBundle] loadNibNamed:@"CountryFlagView" owner:self options:nil] lastObject];
}
//assign data
- (void)setCountryModel:(CountryInfo *)countryModel{
if (_countryModel != countryModel) {
_countryModel = countryModel;
self.name.text = _countryModel.name;
self.flag.image = [UIImage imageNamed:_countryModel.icon];
}
}
+ (CGFloat)rowHeight{
return 54;
}
@end
7 实现代理UIPickViewDataSource的两个必须实现的方法
1)设置有多少个组件
2)设置每个组件有多少行
#pragma mark -UIPickViewDataSource
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return self.dataArray.count;
}
8 设置每行里面的View在代理UIPickViewDelegate中并绑定数据。
#pragma mark - UIPickViewDelegate
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
CountryFlagView *countryView = (CountryFlagView *)view;
if (!countryView) {
countryView = [CountryFlagView countryView];
}
countryView.countryModel = self.dataArray[row];
return countryView;
}