TableView中的日历日期与无限滚动

时间:2016-03-15 07:46:10

标签: ios objective-c date calendar tableview

我正在尝试实施以TableViewCell显示日历日期。我能够实现当年充实。但是,一旦我触及表格的底部,我需要在明年填充,如果我在TableView的顶部,那么应该填充前一年。

粘贴我已实施的代码。

- (void)fillDatesWithCalendarUnit:(NSCalendarUnit)unit withDate:(NSDate*)date
{
    NSDate *today = date;
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSDate *beginning;
    NSTimeInterval length;
    [calendar rangeOfUnit:unit startDate:&beginning interval:&length forDate:today];
    NSDate *end = [beginning dateByAddingTimeInterval:length-1];

    [self fillDatesFromDate:beginning toDate:end];
}

- (void)fillDatesFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
    NSAssert([fromDate compare:toDate] == NSOrderedAscending, @"toDate must be after fromDate");

    NSDateComponents *days = [[NSDateComponents alloc] init];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSInteger dayCount = 0;
    while(YES){
        [days setDay:dayCount++];
        NSDate *date = [calendar dateByAddingComponents:days toDate:fromDate options:0];

        if([date compare:toDate] == NSOrderedDescending) break;
        [_dates addObject:date];
    }
    [self.tableView reloadData]; //_dates mutableArray
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dates.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    AgendaCustomCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.date.text = [NSString stringWithFormat:@"%@",_dates[indexPath.row]];
    if (indexPath.row ==  _dates.count-1) {
        NSLog(@"load more");
        NSDate *tomorrow = [NSDate dateWithTimeInterval:(48*60*60) sinceDate:_dates[indexPath.row]];
        NSLog(@"last daye - %@ ,tomorrow - %@",_dates[indexPath.row],tomorrow);
        [self fillDatesWithCalendarUnit:NSCalendarUnitYear withDate:_dates[indexPath.row]];
    }

    return cell;
}

1 个答案:

答案 0 :(得分:0)

我已经实现了在滚动时显示无限未来日期的逻辑。

- (void)viewDidLoad {
    [super viewDidLoad];
    _dates  = [NSMutableArray new];
    [self fillCurrentYear];
}

- (void)fillCurrentYear
{
    [self fillDatesWithCalendarUnit:NSCalendarUnitYear withDate:[NSDate new
                                                                 ] isLoadMore:NO];
}

pragma mark私有方法

- (void)fillDatesWithCalendarUnit:(NSCalendarUnit)unit withDate:(NSDate*)date isLoadMore:(BOOL)isBool
{
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];
    NSDate *beginning;
    NSTimeInterval length;
    if (isBool) {
        beginning = _dates[[_dates count]-1];
    }
    [calendar rangeOfUnit:unit startDate:&beginning interval:&length forDate:date];
    NSDate *end = [beginning dateByAddingTimeInterval:length-1];

    [self fillDatesFromDate:beginning toDate:end];
}

- (void)fillDatesFromDate:(NSDate *)fromDate toDate:(NSDate *)toDate
{
    NSAssert([fromDate compare:toDate] == NSOrderedAscending, @"toDate must be after fromDate");

//    NSMutableArray *dates = [[NSMutableArray alloc] init];
    NSDateComponents *days = [[NSDateComponents alloc] init];
    NSCalendar *calendar = [NSCalendar autoupdatingCurrentCalendar];

    NSInteger dayCount = 0;
    while(YES){
        [days setDay:dayCount++];
        NSDate *date = [calendar dateByAddingComponents:days toDate:fromDate options:0];

        if([date compare:toDate] == NSOrderedDescending) break;
        [_dates addObject:date];
    }

//    _dates = dates;
    [self.tableView reloadData];
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    AgendaCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.date.text = [NSString stringWithFormat:@"%@",_dates[indexPath.row]];
    if (indexPath.row ==  _dates.count-1) {
        NSLog(@"load more");
        NSDate *tomorrow = [NSDate dateWithTimeInterval:(48*60*60) sinceDate:_dates[indexPath.row]];
        NSLog(@"last daye - %@ ,tomorrow - %@",_dates[indexPath.row],tomorrow);
        [self fillDatesWithCalendarUnit:NSCalendarUnitYear withDate:tomorrow isLoadMore:YES];
    }

    return cell;
}