objective-cios7

How to build UIPIckerView with blocking some days of week on ios7


I want to create picker view with deletion all Mondays on my picker/

How to build uipickerview with blocking some days of week on iOS 7?


Solution

  • You cannot do this with UIDatePicker, you need to make use of UIPickerView and your own logic. Below is the code to achieve that. But not sure whether it is efficient.

    #import "ViewController.h"
    
    @interface ViewController () {
        IBOutlet UIPickerView *_pickerView;
        NSDate *_startDate;
        NSDate *_endDate;
        NSMutableDictionary *_dates;
    }
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad
    {
        [super viewDidLoad];
    
        NSCalendar *calendar = [NSCalendar currentCalendar];//[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
        NSDateComponents *comp = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:[NSDate date]];
        [comp setHour:00];
        [comp setMinute:00];
        [comp setSecond:00];
        [comp setMonth:1];
        [comp setDay:1];
        //From this year
        _startDate = [calendar dateFromComponents:comp];
        [comp setYear:++comp.year];
        //Till next year
        _endDate = [calendar dateFromComponents:comp];
        _dates = [[NSMutableDictionary alloc] initWithObjectsAndKeys:_startDate,@(0), nil];
    }
    
    - (void)didReceiveMemoryWarning
    {
        [super didReceiveMemoryWarning];
    }
    
    #pragma mark - UIPickerViewDataSource
    - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
    {
        return 1;
    }
    
    - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *difference = [calendar components:NSDayCalendarUnit
                                                   fromDate:_startDate toDate:_endDate options:0];
        return [difference day];
    }
    
    - (NSDate *)nextDate:(NSDate *)date
    {
        NSCalendar *calendar = [NSCalendar currentCalendar];
        NSDateComponents *comp = [calendar components:(NSYearCalendarUnit | NSMonthCalendarUnit | NSDayCalendarUnit) fromDate:date];
        [comp setDay:++comp.day];
        return [calendar dateFromComponents:comp];
    }
    
    #pragma mark - UIPickerViewDelegate
    
    - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
    {
        NSDate *currentDate = [_dates objectForKey:@(row)];
    
        if (![_dates objectForKey:@(row+1)]) {
            NSDate *nextDate = [self nextDate:currentDate];
            NSCalendar *calendar = [NSCalendar currentCalendar];
            NSDateComponents *weekdayComponents =[calendar components:NSWeekdayCalendarUnit fromDate:nextDate];
            NSInteger weekday = [weekdayComponents weekday];
            if (weekday == 2) {
                NSDate *nextDateAfterMonday = [self nextDate:nextDate];
                [_dates setObject:nextDateAfterMonday forKey:@(row+1)];
            }
            else{
                [_dates setObject:nextDate forKey:@(row+1)];
            }
        }
        NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
        [formatter setDateFormat:@"EEE, MMM d, ''yy"];
        [formatter stringFromDate:currentDate];
        return [formatter stringFromDate:currentDate];
    }
    @end