美文网首页
UIPickerView-时间选择器

UIPickerView-时间选择器

作者: 守护地中海的花 | 来源:发表于2020-10-23 17:31 被阅读0次
image.png
image.png
image.png

UIPickerView和UITableView用法很相似 下面用一个简单的年月日做为demo

  • 核心api
- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated;
- (NSInteger)selectedRowInComponent:(NSInteger)component;

基础代码

属性

@property(nonatomic,strong)UIPickerView *mainView;

代理

<UIPickerViewDelegate,UIPickerViewDataSource>

懒加载

- (UIPickerView *)mainView
{
    if (!_mainView) {
        UIPickerView *view = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 200*ADAPTER_WIDTH)];
        view.delegate = self;
        view.dataSource = self;
        view.showsSelectionIndicator = YES;
        view.backgroundColor = WhiteColor;
        [self.view addSubview:view];
        _mainView = view;
    }
    return _mainView;
}

代理方法

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return 10;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component
{
    return 44*ADAPTER_WIDTH;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view
{
    if (view == nil) {
        UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, WIDTH - 100*ADAPTER_WIDTH, 44)];
        lab.font = [UIFont systemFontOfSize:15*ADAPTER_WIDTH weight:UIFontWeightRegular];
        lab.textColor = [UIColor blackColor];
        lab.textAlignment = NSTextAlignmentCenter;
        view = lab;
    }
    UILabel *lab = (UILabel *)view;
    lab.text = @"haha";
    return lab;
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    //xxxxx核心滑动逻辑判断
}

业务代码(具体情况具体写)

//数据 包括年 月 日
@property(nonatomic,strong)NSMutableArray<NSMutableArray<NSString *> *> *dataSource;
@property(nonatomic,strong)NSMutableArray<NSString *> *years;
@property(nonatomic,strong)NSMutableArray<NSString *> *months;
@property(nonatomic,strong)NSMutableArray<NSString *> *days;//变化比较大 根据闰年 月份判断普通月份和2月份
//当前的年月日
@property(nonatomic,strong)NSString *year;
@property(nonatomic,strong)NSString *month;
@property(nonatomic,strong)NSString *day;
//现在的年月日
@property(nonatomic,strong)NSString *currentYear;
@property(nonatomic,strong)NSString *currentMonth;
@property(nonatomic,strong)NSString *currentDay;
- (NSMutableArray<NSString *> *)years
{
    if (!_years) {
        _years = [NSMutableArray array];
        for (NSInteger index = 2014; index <= self.currentYear.integerValue; index ++) {
            [_years addObject:[NSString stringWithFormat:@"%ld",index]];
        }
    }
    return _years;
}
- (NSMutableArray<NSString *> *)months
{
    _months = [NSMutableArray array];
    for (NSInteger index = 1; index <= 12; index ++) {
        [_months addObject:[NSString stringWithFormat:@"%ld",index]];
    }
    //判断是否是当年
    if (self.year == self.currentYear) {
        [_months removeAllObjects];
        for (NSInteger index = 1; index <= self.currentMonth.integerValue; index ++) {
            [_months addObject:[NSString stringWithFormat:@"%ld",index]];
        }
    }
    return _months;
}
- (NSMutableArray<NSString *> *)days
{
    NSMutableArray<NSNumber *> *months = [NSMutableArray array];
    [months addObjectsFromArray:@[@31,@28,@31,@30,@31,@30,@31,@31,@30,@31,@30,@31]];
    //判断2月
    NSInteger year = self.year.integerValue;
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        [months replaceObjectAtIndex:1 withObject:@29];
    }
    //获取天数
    NSInteger dayNum = months[(self.month.integerValue - 1)].integerValue;
    NSMutableArray *day = [NSMutableArray array];
    for (NSInteger index = 1; index <= dayNum; index ++) {
        [day addObject:[NSString stringWithFormat:@"%ld",index]];
    }
    //判断是否是当年当月 那么就返回当天的过去的
    if (self.year == self.currentYear && self.month == self.currentMonth) {
        [day removeAllObjects];
        for (NSInteger index = 1; index <= self.currentDay.integerValue; index ++) {
            [day addObject:[NSString stringWithFormat:@"%ld",index]];
        }
    }
    return day;
}
- (void)createDataSource
{
    NSDate *date = [NSDate date];
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"YYYY-MM-dd"];
    NSString *currentTime = [formatter stringFromDate:date];
    NSArray<NSString *> *timeArrs = [currentTime componentsSeparatedByString:@"-"];
    self.year = timeArrs[0];
    self.month = [NSString stringWithFormat:@"%ld",timeArrs[1].integerValue];
    self.day = [NSString stringWithFormat:@"%ld",timeArrs[2].integerValue];
    self.currentYear = self.year;
    self.currentMonth = self.month;
    self.currentDay = self.day;
    
    [self.dataSource addObject:self.years];
    [self.dataSource addObject:self.months];
    [self.dataSource addObject:self.days];
    [self.mainView reloadAllComponents];
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.mainView selectRow:[self.years indexOfObject:self.currentYear] inComponent:0 animated:YES];
        [self.mainView selectRow:[self.months indexOfObject:self.currentMonth] inComponent:1 animated:YES];
        [self.mainView selectRow:[self.days indexOfObject:self.currentDay] inComponent:2 animated:YES];
    });
}

因为我这个业务是不能超过当前年月日所有 月和日的数组是动态的获取

滑动核心

  • 滑动年
    滑动年 停止后要刷新月 月如果是超出范围限制内 需要刷新日
if (component == 0) {
xxxxxx
} else if (component == 1) {
} else {
}

//获取当前年
self.year = self.dataSource[component][row];
//替换月的数据
[self.dataSource replaceObjectAtIndex:1 withObject:self.months];
//UI刷新 --- 因为有的年份月可能有限制比如当年不超过本月(如现在2020年5月 那么2020年月份就是1 2 3 4 5 不能超过5)
[pickerView reloadComponent:1];
//月刷新后 看日
dispatch_async(dispatch_get_main_queue(), ^{
    //获取当前月
    self.month = self.dataSource[1][[pickerView selectedRowInComponent:1]];
    //获取当前月有多少日
    [self.dataSource replaceObjectAtIndex:2 withObject:self.days];
    //UI --- 刷新日
    [pickerView reloadComponent:2];
    //刷新完日 界面停止 这个时候才能真正获取到当前pickerView上的数据
    dispatch_async(dispatch_get_main_queue(), ^{
        //联动后
        self.day = self.dataSource[2][[pickerView selectedRowInComponent:2]];
        //显示UI
        //xxxxxx
    });
});

核心

dispatch_async(dispatch_get_main_queue(), ^{
    dispatch_async(dispatch_get_main_queue(), ^{
        dispatch_async(dispatch_get_main_queue(), ^{
            
        });
    });
});
  • 滑动月
  • 滑动日

相关文章

网友评论

      本文标题:UIPickerView-时间选择器

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