-2
내 픽커에서 모든 월요일을 삭제하여 선택기보기를 만들고 싶습니다. 아무도 도와 줄 수 있습니까? ios7에서 요일을 차단하여 UIPIckerView를 작성하는 방법
방법 iOS7에 강력한 텍스트 당신은 UIDatePicker이 작업을 수행 할 수내 픽커에서 모든 월요일을 삭제하여 선택기보기를 만들고 싶습니다. 아무도 도와 줄 수 있습니까? ios7에서 요일을 차단하여 UIPIckerView를 작성하는 방법
방법 iOS7에 강력한 텍스트 당신은 UIDatePicker이 작업을 수행 할 수에 일주일에 몇 가지 일을 차단하여 uipickerview 구축, 당신은 UIPickerView하고 자신의 논리의 사용을 확인해야합니다. 아래는이를 달성하기위한 코드입니다. 그러나 그것이 효율적인지 확실하지 않습니다.
#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
감사합니다. 너무 많은 사람, 올바르게 작동합니다. –
일주일 동안 선택기를 만들고 있습니까? –
아래 URL을 확인하십시오. 도움이 될 수 있습니다. http://stackoverflow.com/questions/12206336/how-to-block-days-in-uidatepicker-for-ios – Saty
아니요 모든 캘린더 용이지만 캘린더에서 월요일을 피하려고합니다. –