我需要创建一个自定义datepicker
,其中第一列显示日期名称、日期编号,第二列显示月份名称和年份。
本月效果很好,但每隔一个月,每月的第一天总是星期一。
#pragma Pickerview
-(void)initilazePickerview
{
//set our picker array data
self.yearAndMonth = [[NSMutableArray alloc] init];
self.day= [[NSMutableArray alloc] init];
self.backendYearAndMonth= [[NSMutableArray alloc] init];
self.backendDay= [[NSMutableArray alloc] init];
/*
next 12 months after current month
*/
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *today = [NSDate date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:today];
int currentMonth = [monthComponents month];
NSDateComponents *yearComponents = [currentCalendar components:NSYearCalendarUnit fromDate:today];
int currentYear = [yearComponents year];
int nextYear = currentYear + 1;
int months = 1;
int year;
for(int m = currentMonth-1; months <= 12; m++){
int nextMonth = m % 12;
if(nextMonth < currentMonth-1){
year = nextYear;
} else {
year = currentYear;
}
//NSLog(@"%@ %i",[[dateFormatter shortMonthSymbols] objectAtIndex: nextMonth],year);
NSString *populateYearAndMonth=[NSString stringWithFormat:@"%@ %i",[[dateFormatter shortMonthSymbols] objectAtIndex: nextMonth],year];
[self.yearAndMonth addObject:populateYearAndMonth];
//save year and month in array
NSMutableDictionary *saveYearMonth= [[NSMutableDictionary alloc] init];
[saveYearMonth setObject:[NSNumber numberWithInt:nextMonth] forKey:@"month"];
[saveYearMonth setObject:[NSNumber numberWithInt:year] forKey:@"year"];
[self.backendYearAndMonth addObject:saveYearMonth];
months++;
}
[self populateDays:currentMonth-1 year:currentYear];
}
-(void) populateDays:(int) populateMonth year:(int) populateYear
{
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSDate *today = [NSDate date];
NSCalendar *currentCalendar = [NSCalendar currentCalendar];
NSDateComponents *monthComponents = [currentCalendar components:NSMonthCalendarUnit fromDate:today];
int currentMonth = [monthComponents month];
if (populateMonth==currentMonth-1) {
NSLog(@"Today");
[self.day addObject:@"Today"];
[self.backendDay addObject:today];
NSRange days = [currentCalendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:today];
NSLog(@"days.length = %i",days.length);
NSDateComponents *dayComponents = [currentCalendar components:NSDayCalendarUnit fromDate:today];
int currentDay = [dayComponents day];
for (int i=currentDay+1; i<=days.length; i++) {
int daySymbol=i%7;
NSString *populateDay=[NSString stringWithFormat:@"%@ %i",[[dateFormatter shortWeekdaySymbols] objectAtIndex: daySymbol],i];
[self.day addObject:populateDay];
}
}
else
{
NSDateComponents* comps = [[NSDateComponents alloc] init];
// Set your month here
[comps setMonth:populateMonth+1];
NSRange days = [currentCalendar rangeOfUnit:NSDayCalendarUnit
inUnit:NSMonthCalendarUnit
forDate:[currentCalendar dateFromComponents:comps]];
NSLog(@"days.length = %i",days.length);
NSDateComponents *dayComponents = [currentCalendar components:NSDayCalendarUnit fromDate:[currentCalendar dateFromComponents:comps]];
int currentDay = [dayComponents day];
for (int i=currentDay; i<=days.length; i++) {
int daySymbol=i%7;
NSString *populateDay=[NSString stringWithFormat:@"%@ %i",[[dateFormatter shortWeekdaySymbols] objectAtIndex: daySymbol],i];
[self.day addObject:populateDay];
}
}
[self.myCustomPicker reloadComponent:0];
}
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
if (component == 1)
{
[self.day removeAllObjects];
[self.backendDay removeAllObjects];
NSDictionary *dataDict= [self.backendYearAndMonth objectAtIndex:row];
int month=[[dataDict objectForKey:@"month"]intValue];
int year=[[dataDict objectForKey:@"year"]intValue];
[self populateDays:month year:year];
}
}
// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
NSInteger result = 0;
if ([pickerView isEqual:self.myCustomPicker]){
result = 2;
}
return result;
}
// returns the number of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component{
NSInteger result = 0;
if ([pickerView isEqual:self.myCustomPicker]){
switch (component) {
case 0:
result = [self.day count];
break;
case 1:
result = [self.yearAndMonth count];
break;
default:
break;
}
}
return result;
}
//return a plain NSString to display the row for the component.
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row
forComponent:(NSInteger)component{
NSString *result = nil;
if ([pickerView isEqual:self.myCustomPicker]){
switch (component) {
case 0:
result = [self.day objectAtIndex:row];
break;
case 1:
result = [self.yearAndMonth objectAtIndex:row];
break;
default:
break;
}
}
return result;
}
有什么建议可以解决这个问题吗?谢谢空间,