UiCollectionView显示上一个/下个月的分页

时间:2017-09-19 09:13:34

标签: objective-c uicollectionview nsdate ios11

我需要一点帮助...... 我有一个自定义的UIVIew类,里面有一个collectionView,显示每月每天的日历。

我的collectionView完美运行,在collectionView的顶部有两个UIButtons,允许用户显示下个月或前几个月。

现在,我希望看到下一天或前几天用户可以通过滚动浏览collectionView来查看它们,方法是将每个月与分页分开。

在这里,我向您展示用于浏览月份的代码

    #pragma mark -
#pragma mark  NEXT / PREVIOUS MONTH FUNCTION
-(NSDate *)nextMonth {
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [[NSDateComponents alloc] init];
    dateComponents.month = 1;
    return [calendar dateByAddingComponents:dateComponents toDate:self.currentMonth options:0];
}

- (NSDate *)previousMonth {
    NSInteger addValue = -1;
    NSCalendar *calendar = [NSCalendar currentCalendar];
    NSDateComponents *dateComponents = [NSDateComponents new];
    dateComponents.month = addValue;
    return [calendar dateByAddingComponents:dateComponents toDate:self.currentMonth options:0];
}

- (IBAction)prev:(id)sender {
    self.currentMonth = [self previousMonth];
    [_calendarCollectionView reloadData];
}

- (IBAction)add:(id)sender {
    self.currentMonth = [self nextMonth];
    [_calendarCollectionView reloadData];
}

以下是我的UICollectionView

的方法
#pragma mark -
#pragma mark  COLLECTIONVIEW DATA SOURCE
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
   // Calcola il numero delle settimane
    NSRange rangeOfWeeks = [[NSCalendar currentCalendar] rangeOfUnit:NSCalendarUnitWeekOfMonth inUnit:NSCalendarUnitMonth forDate:[self firstDayOfMonth]];
    NSUInteger numberOfWeeks = rangeOfWeeks.length;
    NSInteger numberOfItems = numberOfWeeks * 7;

    return numberOfItems;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellID = @"dayCell";

    UPCalendarCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];

    NSDateFormatter *formatter = [NSDateFormatter new];
    formatter.dateFormat = @"d";

    cell.dayLabel.text = [formatter stringFromDate:[self dateForCellAtIndexPath:indexPath]];

    // Trova il giorno corrente e cerchialo
    [cell highlitedDay];

    // Trova tutti i giorni PRIMA del primo giorno del Mese ed evidenziali
    if ([self dateForCellAtIndexPath:indexPath] < self.firstDayOfMonth) cell.dayLabel.textColor = [self disableDayColor];


    // Trova tutti i giorni DOPO l'ultimo giorno del Mese ed evidenziali
    if ([self dateForCellAtIndexPath:indexPath] > [self lastDayOfMonth])  cell.dayLabel.textColor = [self disableDayColor];

    return cell;
}

我怎样才能得到这一切?你能用这个例子帮助我吗?

0 个答案:

没有答案