一、簡介
UIPickView是在iOS開發者經常用到的一種控件,經常用於城市選擇,日期選擇等。
二、初始化
UIPickerView * PickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 40, screenW, 260)];
PickerView.delegate = self;
PickerView.dataSource = self;
PickerView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:PickerView];
三、UIPickView
// 是否顯示指示器,默認爲NO
@property(nonatomic) BOOL showsSelectionIndicator;
// 列數
@property(nonatomic,readonly) NSInteger numberOfComponents;
// 返回某一列有多少行
- (NSInteger)numberOfRowsInComponent:(NSInteger)component;
// 返回某一列的大小
- (CGSize)rowSizeForComponent:(NSInteger)component;
// 某一列某一行的標題控件
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
// 重新加載所有列(UIPickView)
- (void)reloadAllComponents;
// 重新加載某一列
- (void)reloadComponent:(NSInteger)component;
// 選中到某一列某一行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
// 某一列的行數
- (NSInteger)selectedRowInComponent:(NSInteger)component;
四、UIPickerViewdataSource方法
// 一共有多少組(列)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
// 每列有多少行,傳入的參數component是對應的列
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
五、UIPickerViewDelegate
// 列寬
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
// 行高
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
// 返回字符串類型的文本標題
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
// 返回字符屬性的字符串類型的文本標題,可改變返回的字符串顏色,但不能改變大小(測試過了)
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component;
// 返回view類型標題控件
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view;
// 選中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
六、代碼示例-> .m文件,繼承於UIPickView類(兩列)
#import "PickKeyBoardView.h"
@interface PickKeyBoardView () <UIPickerViewDataSource, UIPickerViewDelegate>
@property (nonatomic, retain) NSMutableArray * plarTypeArr;
@property (nonatomic, assign) NSInteger selectRowOfCom1;
@end
@implementation PickKeyBoardView
- (instancetype)init {
self = [super init];
if (self) {
self.dataSource = self;
self.delegate = self;
_plarTypeArr = [self CityArr];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
self.dataSource = self;
self.delegate = self;
_plarTypeArr = [self CityArr];
}
return self;
}
#pragma mark - UIPickerViewDataSource
// 列數
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 2;
}
// 行數
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
if (component == 0) {
return self.plarTypeArr.count;
}
else {
NSDictionary * dict = [self.plarTypeArr objectAtIndex:self.selectRowOfCom1];
NSArray * playArr = [dict objectForKey:@"key2"];
return playArr.count;
}
}
// 列寬
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
return screenW / 2;
}
// 標題
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
UILabel * label = (UILabel *)view;
if (label == nil) {
CGRect frame = CGRectMake(0.0, 0.0, screenW / 2, 40);
label = [[UILabel alloc] initWithFrame:frame];
[label setTextAlignment:NSTextAlignmentCenter];
[label setFont:[UIFont systemFontOfSize:15.0f]];
}
if (component == 0) {
label.backgroundColor = [UIColor greenColor];
NSDictionary * dict = [self.plarTypeArr objectAtIndex:row];
label.text = [dict valueForKey:@"key1"];
return label;
}else {
label.backgroundColor = [UIColor yellowColor];
NSDictionary * dict = [self.plarTypeArr objectAtIndex:self.selectRowOfCom1];
NSArray * playArr = [dict valueForKey:@"key2"];
label.text = [playArr objectAtIndex:row];
return label;
}
}
// 選中
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 0) {
self.selectRowOfCom1 = row;
[pickerView reloadComponent:1];
[pickerView selectRow:0 inComponent:1 animated:NO];
}
}
- (NSArray *)CityArr {
NSArray * array = @[@"福建省",@[@"泉州",@"福州",@"莆田",@"三明",@"廈門"],
@"廣東省",@[@"廣州",@"深圳",@"珠海",@"東莞",@"佛山"]];
NSInteger i = 0;
NSMutableArray * muArr = [[NSMutableArray alloc] init];
while (i < array.count) {
NSString * str = [array objectAtIndex:i];
NSArray * arr = [array objectAtIndex:i+1];
NSDictionary * dict = [[NSDictionary alloc] initWithObjectsAndKeys:str, @"key1", arr, @"key2", nil];
[muArr addObject:dict];
i = i + 2;
}
return muArr;
}
@end