高光细胞集合

时间:2017-09-08 15:00:39

标签: ios objective-c uicollectionview nsdate

我已经使用集合视图创建了一个日历,但是今天我需要在集合视图中突出显示。

我会解释。

今天是9月8日。在我的collectionView中,我希望包含数字8的单元格用红色填充。

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath {
    static NSString *cellId = @"dayCell";
    static BOOL xibLoaded = NO;

    if (!xibLoaded) {
        UINib *nib = [UINib nibWithNibName:@"UPCalendarCell" bundle:nil];
        [collectionView registerNib:nib forCellWithReuseIdentifier:cellId];
        xibLoaded = YES;
    }

    UPCalendarCell *cell = (UPCalendarCell *)[collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];

    /***** ARRAY WITH WEEKDAY *****/
    NSString *cellData = [_day objectAtIndex:indexPath.row];
    cell.weekday.text = [NSString stringWithFormat:@"%@", cellData];

    /***** ARRAY WITH DAY NUMBER *****/
    NSString *day = [_numberDay objectAtIndex:indexPath.row];
    cell.day.text = [NSString stringWithFormat:@"%@", day];

    /*******************************************
     *******************************************
     if today's number is in the collection view, highlight the corresponding cell
     *******************************************
     *******************************************/

    NSDate *currentDate = [NSDate date]; // CURRENT DATE
    NSCalendar* calendar = [NSCalendar currentCalendar];  // CURRENT CALENDAR
    NSDateComponents* components = [calendar components:NSCalendarUnitDay fromDate:currentDate]; // FETCH CURRENT DAY NUMBER

    NSInteger numbers = [components day];
    NSNumber *number = [NSNumber numberWithInteger:numbers];

    if ([_numberDay containsObject:number]) {
        NSLog(@"YES");
    } else {
        NSLog(@"NO");
    }

    return cell;
}

0 个答案:

没有答案