美文网首页iOS经验总结
IOS-UIPickerView和UIDatePicker详解

IOS-UIPickerView和UIDatePicker详解

作者: 街角仰望 | 来源:发表于2018-07-06 14:50 被阅读103次

UIPickerViewUIDatePicker使用起来相对比较简单,下面通过简单例子深入掌握它们。

UIPickerView

属性

   // 数据源(用来告诉UIPickerView有多少列多少行)
    @property(nonatomic,assign) id<UIPickerViewDataSource> dataSource;
    
    // 代理(用来告诉UIPickerView每1列的每1行显示什么内容,监听UIPickerView的选择)
    @property(nonatomic,assign) id<UIPickerViewDelegate>   delegate;
    
    // 是否要显示选中的指示器
    @property(nonatomic)   BOOL   showsSelectionIndicator;
    
    // 一共有多少列
    @property(nonatomic,readonly) NSInteger numberOfComponents;

方法

// 重新刷新所有列
- (void)reloadAllComponents;
// 重新刷新第component列
- (void)reloadComponent:(NSInteger)component;
 
// 主动选中第component列的第row行
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
 
// 获得第component列的当前选中的行号
- (NSInteger)selectedRowInComponent:(NSInteger)component;

数据源方法

//  一共有多少列
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView;
//  第component列一共有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;

代理方法

//  第component列的宽度是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component;
//  第component列的行高是多少
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component;
 
//  第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;
 
//  选中了pickerView的第component列第row行
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component;

例子(联动效果)

创建UIPickerView,设置代理和数据源,添加到view

    UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0,64,320, 200)];
    pickerView.dataSource = self;
    pickerView.delegate = self;
    
    [self.view addSubview:pickerView];

懒加载省市数据

@property (nonatomic,strong) NSArray *provinces;
 
 
-(NSArray *)provinces{
    if (_provinces == nil) {
        NSMutableArray *provincesArr = [NSMutableArray array];
        NSArray *arr = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cities.plist" ofType:nil]];
        for (NSDictionary *dict in arr) {
            ZXHProvince *province = [ZXHProvince provinceWithDict:dict];
            [provincesArr addObject:province];
        }
        _provinces = provincesArr;
    }
    return _provinces;
}

实现数据源方法。返回多少列,每一列多少行

#pragma mark 数据源
//返回多少列
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
     NSLog(@"==数据源==numberOfComponentsInPickerView:");
    return 2;
}
 
//返回每一列多少行
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSLog(@"==数据源===numberOfRowsInComponent");
    if (component==0) {//省份
        return self.provinces.count;
    }else{//市
        //获得选中了哪一个省
        NSInteger index = [pickerView selectedRowInComponent:0];
        ZXHProvince *province = self.provinces[index];
        NSArray *cities = province.cities;
        return cities.count;
        
    }
}

实现代理方法。注意:监听省份的时候要刷新第二列城市,重新设置城市数据

#pragma mark 代理方法
//显示的数据
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSLog(@"== 代理方法===titleForRow=====%ld",row);
  
    if (component==0) {
        ZXHProvince *province = self.provinces[row];
        return province.name;
    }else{
        //获得选中了哪一个省
        NSInteger index = [pickerView selectedRowInComponent:0];
        ZXHProvince *province = self.provinces[index];
        return province.cities[row];
    }
}
 
/**
 * 监听选中了某一列的某一行
 **/
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    if (component==0) {//省份
        // 刷新第1列的数据(重新刷新数据,重新调用数据源和代理的相应方法获得数据)
        [pickerView reloadComponent:1];
        //默认显示
        [pickerView selectRow:0 inComponent:1 animated:YES];
    }
    
    //更改显示文字
    //选择省份
    ZXHProvince *province;
    NSInteger pIndex = [pickerView selectedRowInComponent:0];
    province = self.provinces[pIndex];
    NSString *name = province.name;
    //城市
    NSInteger cIndex = [pickerView selectedRowInComponent:1];
    NSString *city = province.cities[cIndex];
    
    self.showTextLable.text = [NSString stringWithFormat:@"省份:%@   城市:%@",name,city];
}

模型数据代码(略)

效果图

自定义view

既可显示文字组也可显示自定义view。当要在行中显示view,则实现一下代理方法,返回自定义view。

#pragma mark 代理方法
/**
 *  第component列的第row行显示怎样的view
 *  每当有一行内容出现在视野范围内,就会调用(调用频率高) 不用使用标识
 */
 
-(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
   
//    UIView *v = [[UIView alloc]init];
//    v.backgroundColor = [UIColor redColor];
//    v.frame =CGRectMake(0, 0,320,100);
//    
//    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, 20, 100,30)];
//    [btn setTitle:@"test" forState:UIControlStateNormal];
//    [UIButton buttonWithType:UIButtonTypeContactAdd];
//    [v addSubview:btn];
//    return v;
    ZXHFlagView *flagView = [ZXHFlagView flagViewWithreusingView:view];
    flagView.flag = self.flags[row];
    return flagView;
    
}

效果图


UIDatePicker

属性

// datePicker的显示模式
@property (nonatomic) UIDatePickerMode datePickerMode;
// 显示的区域语言
@property (nonatomic, retain) NSLocale   *locale;

监听UIDatePicker的选择

因为UIDatePicker继承自UIControl,所以通过addTarget:...监听

使用

创建UIDatePicker

UIDatePicker *datePicker = [[UIDatePicker alloc]init];

设置日期格式和语言

/*
  UIDatePickerModeTime,     // Displays hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. 6 | 53 | PM)

  UIDatePickerModeDate,           // Displays month, day, and year depending on the locale setting (e.g. November | 15 | 2007)

  UIDatePickerModeDateAndTime,    // Displays date, hour, minute, and optionally AM/PM designation depending on the locale setting (e.g. Wed Nov 15 | 6 | 53 | PM)

  UIDatePickerModeCountDownTimer, // Displays hour and minute (e.g. 1 | 53)
*/

datePicker.datePickerMode = UIDatePickerModeDate;
datePicker.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"];

监听选择的日期时间

[datePicker addTarget:self action:@selector(birthdayChange:) forControlEvents:UIControlEventValueChanged];

添加到view显示

 [self.view addSubview:datePicker];

效果图

参考:https://blog.csdn.net/zhixinhuacom/article/details/49643753

相关文章

  • IOS-UIPickerView和UIDatePicker详解

    UIPickerView和UIDatePicker使用起来相对比较简单,下面通过简单例子深入掌握它们。 UIPic...

  • iOS-UIPickerView详解

    iOS-UIPickerView详解 // pickView初始化并设置其大小,如果不设置其大小,默认大小为 32...

  • iOS-UIPickerView 详解总结

    写在文前 由于最近开发中经常碰到类似日期选择器相关业务使用场景,虽然这个系统控件相对来说非常简单,有点儿类似UIT...

  • UIPickView和UIDatePicker

    UIPickView和UIDatePicker UIPickView 样式 作用 常见用法 注意 显示数据 UID...

  • UIDatePicker 和 UIPickerView

    UIDatePicker UIDatePicker:是一个控制器类,封装了 UIPickerView,但是他是UI...

  • UIPickerView 和 UIDatePicker

    UIDatePicker UIPickerView 思路 几列 每列里面包含了几行 每行里面的数据 实现协议

  • UIPickView和UIDatePicker

    一、介绍UIPickView和UIDatePicker 1.UIPickView什么时候用? •通常在注册模块,当...

  • UIDatePicker和UIToolBar

    DatePicker显示时间的控件:有默认宽度,不用设置数据源和代理; 如何改成中文的? 1> 查看当前系统是否为...

  • iOS备忘录

    1. 设置UIDatePicker 12小时制和24小时制 UIDatePicker默认是根据系统设置中的12小时...

  • UIDatePicker简述

    // UIDatePicker控件的常用方法 时间选择控件 UIDatePicker *oneDatePicker...

网友评论

    本文标题:IOS-UIPickerView和UIDatePicker详解

    本文链接:https://www.haomeiwen.com/subject/szavuftx.html