Search code examples
iosuiviewuicollectionviewnsdatensindexpath

UICollectionView does not return the cells correctly


I'm creating a calendar with a collectionView ... So far I get all the WEEKDAY perfectly but I have problems with the numbered days. The collectionView always returns me 31 days without checking whether the month contains 30 or 31 days .. Where am I wrong? some mistake of inattention? can you help me? this is the code I use to get the calendar

+(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;
    }
}

Solution

  • Maybe...

    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]];
    

    It looks like you want to use date from line 1, but you are passing a new date object in line 8 ([NSDate date] which == today). Should line 8 be:

    NSInteger numberOfDay = [KPSmallCalendarDayData dayOfMonthFromDate:date];