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;
}
Привет Sandy. Вам действительно нужны бесконечные даты? Я думаю, что большинство людей будут более чем удовлетворены, если они скажут 100 лет или даже меньше! Тогда вам не нужно беспокоиться о бесконечной проблеме. Вы можете рассчитать другое фиксированное количество лет. –
Мне нужно почти 100 лет, иначе мудрый он потерпит неудачу при тестировании автоматизации. правильно? – Sandy