2016-03-15 6 views
0

Я пытаюсь реализовать, чтобы показать даты календаря в TableViewCell. Я могу достичь заполненного в текущем году. Но как только я попаду в нижнюю часть таблицы, мне нужно заполнить следующий год, и если я удалю в верхней части TableView, тогда в прошлом году нужно будет заселить.Календарь Даты в 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; 
} 
+0

Привет Sandy. Вам действительно нужны бесконечные даты? Я думаю, что большинство людей будут более чем удовлетворены, если они скажут 100 лет или даже меньше! Тогда вам не нужно беспокоиться о бесконечной проблеме. Вы можете рассчитать другое фиксированное количество лет. –

+0

Мне нужно почти 100 лет, иначе мудрый он потерпит неудачу при тестировании автоматизации. правильно? – Sandy

ответ

0

Я реализовал логику отображения бесконечных будущих дат при прокрутке.

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

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

Прагма знак Частные методы

- (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; 
} 
Смежные вопросы