2016-03-15 60 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

桑迪你好。 你真的需要無限的約會嗎?我認爲如果他們說了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; 
}