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;
}
桑迪你好。 你真的需要无限的约会吗?我认为如果他们说了100年甚至更少,大多数人会感到满意!那么你不必担心无限的问题。你可以计算另一个固定的年数。 –
我需要近100年的时间,其他方面它会在自动测试中失败。对? – Sandy