remove/hide rows outside minimum/maximum date range of UIDatePicker?

后端 未结 1 1853
情深已故
情深已故 2021-02-06 02:24

I have a UIDatePicker with set minimum and maximum dates. I\'m wondering if there is a way to hide the rows of the columns for the dates/times that are either befor

1条回答
  •  伪装坚强ぢ
    2021-02-06 03:08

    Using the minimumDate and maximumDate APIs will behave as you've explained - still show the dates but not allow selecting them. There is currently no way to hide those dates that are out of the provided range, however I do have a solution for you.

    Instead of using the min and max dates with a UIDatePicker, you can generate an array of all of the NSDates you want to show to the user, and use a UIPickerView to present them to the user. I've done exactly this for one of my own apps.

    self.datePicker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 162)];
    self.datePicker.dataSource = self;
    self.datePicker.delegate = self;
    //...
    
    #pragma mark - UIPickerView Data Source and Delegate
    
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
        return [self.availableDates count];
    }
    
    - (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component {
        return 28;
    }
    
    - (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectZero];
        label.font = [UIFont systemFontOfSize:20];
        label.textColor = [UIColor blackColor];
    
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        dateFormatter.dateFormat = @"EEE, MMMM d";
    
        label.text = [dateFormatter stringFromDate:self.availableDates[row]];
    
        [label sizeToFit];
    
        return label;
    }
    

    0 讨论(0)
提交回复
热议问题