UIPickerView 属于UIView的子类 UIDataPicker时间类
UIPickerView简介:
UIPickerView是一个 拾取器,UIPickerView和UIDatePicker是类似的控件,只不过UIDatePicker是日期控件,只能放日期,而UIPickerView可以放任何东西。它比UIDatePicker更加通用,它可以生成单列的选择器,也可生成多列的选择器,而且开发者完全可以自定义选择项的外观,因此用法非常灵活。UIPickerView直接继承了UIView,没有继承UIControl,因此,它不能像UIControl那样绑定事件处理方法,UIPickerView的事件处理由其委托对象完成。使用UIPickerView的对象应该遵守UIPickerViewDataSource,UIPickerViewDelegate这两个代理。
UIPickerView的具体实现步骤:
1⃣️创建视图 初始化UIPickerView
2⃣️导入两个代理<UIPickerViewDelegate,UIPickerViewDataSource>
3⃣️挂上代理
4⃣️实现代理
🈚️显示视图
下面以一个简单的例子来介绍它 实现的功能:
#import "ViewController.h"
@interface ViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>//2⃣️导入两个代理
{
NSArray *dataarray;
NSArray *dataarray1;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//数据源数组
dataarray = @[@"北京",@"上海",@"深圳",@"厦门",@"长沙",@"成都"];
dataarray1 = @[@"01",@"02",@"03",@"04",@"05",@"06"];
// 1⃣️创建视图 初始化UIPickerView
UIPickerView *pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(120, 240, 200, 180)];
// 3⃣️挂上代理
pickerView.dataSource = self;
pickerView.delegate = self;
[self.view addSubview:pickerView];
}
//4⃣️实现代理
#pragma mark -----数据源UIPickerViewDataSource代理的方法-
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 2;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return dataarray.count;
}
#pragma mark -----数据源UIPickerViewDelegate代理的方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
NSString *alertString = [NSString stringWithFormat:@"row%ld component%ld",row+1,component+1];
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选中" message:alertString preferredStyle:UIAlertControllerStyleAlert];
// 添加按钮
UIAlertAction *alertButton = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
}];
//按钮添加到弹出框
[alertController addAction:alertButton];
//显示出来
[self presentViewController:alertController animated:YES completion:^{
}];
}
//返回指定行的标题
- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
if (component == 0) {
return dataarray[row];
}else{
return dataarray1[row];
}
}
它还有其他的方法,可以自己研究一下。
// 设置分组的宽
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
return 1;
}
//设置单元格的高 - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
return 1;
}
//获取指定分组component中的行数
- (NSInteger)numberOfRowsInComponent:(NSInteger)component{
// return _array.count;
return nil;
}
//获取单元格的Size - (CGSize)rowSizeForComponent:(NSInteger)component{
}
//获取pickerView:viewForRow:forComponent:reusingView:中定义的View,当pickerView:viewForRow:forComponent:reusingView:未实现或者行或分组不可用时返回nil
- (UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component;
//刷新所有分组
- (void)reloadAllComponents;
//刷新指定分组
- (void)reloadComponent:(NSInteger)component;
//选中指定分组中的一行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
//获得指定分组中选中的行
(NSInteger)selectedRowInComponent:(NSInteger)component;
(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
return 1;
}
//必须实现的方法(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
// NSString *string = _array[row];
// return string;
return nil;
}(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
// self.selectItem = _array[row];
}
单元格中内容的展示,以下三种方法三选一
//设置显示普通字符串
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component;
//设置显示属性字符串,如果该方法和普通字符串方法都实现了,效果是属性字符串
- (NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0);
//自定义显示视图
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view;
单元格选中时的委托方法,需要注意的是,单元格的值改变后停止滚动动画停止即调用这个方法,不需要手点击
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;
我只是简单的写了,写的不好,请谅解。