我正在创建一个带有collectionView的日历......到目前为止,我完全可以获得所有WEEKDAY,但我在编号的日子里遇到了问题。 collectionView总是返回我31天,而不检查月份是否包含30或31天..我哪里错了?一些注意力不集中的错误?你能帮助我吗?这是我用来获取日历的代码
+(NSInteger)dayOfMonthFromDate:(NSDate *)date {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSRange range = [calendar rangeOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitMonth forDate:date];
NSInteger numberOfDayInMonth = range.length;
return numberOfDayInMonth;
}
+(void)buildSmartCalendarDayWithWeekArray:(NSMutableArray *)week andDay:(NSMutableArray *)day {
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDate *now = [NSDate date];
/***** YEAR ****/
NSInteger year = [calendar component:NSCalendarUnitYear fromDate:now];
/**** MONTH ****/
NSInteger month = [calendar component:NSCalendarUnitMonth fromDate:now];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
components.month = month+4;
components.year = year;
NSDate *date = [calendar dateFromComponents:components];
NSDateComponents *compWeek = [calendar components:NSCalendarUnitWeekday fromDate:date];
NSInteger weekDay = [compWeek weekday];
/******* Numero di giorni del mese in corso *******/
/*************************************************/
NSInteger numberOfDay = [KPSmallCalendarDayData dayOfMonthFromDate:[NSDate date]];
//------------------------------------------------------------------//
/******* Recupera in quale giorno siamo *******/
/*************************************************/
NSUInteger todayWeekDay = weekDay-1;
/******* Crea un ciclo for per la suddivisione di ogni giorno del mese in forma testuale *******/
/*****************************************************************************/
for (NSInteger initialDay = 1; initialDay <= numberOfDay; initialDay ++) {
// Crea l'array contenente tutti giorni del mese dal LUN alla DOM
NSDateFormatter *dayFormat = [[NSDateFormatter alloc] init];
NSString *dayString = [NSString stringWithFormat:@"%@", [[dayFormat shortWeekdaySymbols] objectAtIndex:todayWeekDay]];
NSString *uppercase = [dayString uppercaseString];
[week addObject:uppercase];
todayWeekDay ++;
// Crea l'array contenente la numerazione delle celle
NSNumber *totalDay = [NSNumber numberWithInteger:initialDay];
[day addObject:totalDay];
if (todayWeekDay >6) todayWeekDay = 0;
}
}
答案 0 :(得分:1)
也许......
1. NSDate *date = [calendar dateFromComponents:components];
2. NSDateComponents *compWeek = [calendar components:NSCalendarUnitWeekday fromDate:date];
3.
4. NSInteger weekDay = [compWeek weekday];
5.
6. /******* Numero di giorni del mese in corso *******/
7. /*************************************************/
8. NSInteger numberOfDay = [KPSmallCalendarDayData dayOfMonthFromDate:[NSDate date]];
您似乎想要使用第1行中的date
,但是您要在第8行传递新 date
对象([NSDate date]
==今天)。第8行应该是:
NSInteger numberOfDay = [KPSmallCalendarDayData dayOfMonthFromDate:date];