我在这里要完成的是使用带有原型单元格的tableView,并在每个单元格中显示一年或两年的日期。我已经通过使用以下代码在标签中显示今天的日期:
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
NSDate *dateNow = [NSDate date];
dateArray = [NSMutableArray arrayWithObjects:[dateFormatter stringFromDate:dateNow], nil];
它可以向第一个单元格添加一个日期(今天的日期)。我无法弄清楚如何让它以1的间隔添加所有日子。任何帮助?
我使用以下代码显示数组中的信息:
static NSString *CellIdentifier = @"Cell";
Custom *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.dateLabel.text = [dateArray objectAtIndex:indexPath.row];
答案 0 :(得分:0)
你走了,
在viewDidload中,
dateArray = [[NSMutableArray alloc] init]; //dateArray is global
NSDate *thisDate, *nextDate;
thisDate = [NSDate date];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"MM/dd/yyyy"];
[dateArray addObject: [dateFormatter stringFromDate:thisDate]];
NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
components.day = 1;
for (int i = 0; i<365; i++) //Not considering leap years
{
nextDate = [gregorian dateByAddingComponents:components toDate:thisDate options:0];
[dateArray addObject:[dateFormatter stringFromDate:nextDate]];
thisDate = nextDate;
}
在cellForRowAtIndexPath
中cell.dateLabel.text = [dateArray objectAtIndex:indexPath.row];
答案 1 :(得分:0)
您无需维护日期数组。如果要显示 n th 行,您只需将 n 天添加到开始日期并使用它。
所以你的开始日期存储在一个实例变量中:
NSDate *_startDate;
要确定tableView中应该有多少行,你必须弄清楚那一年有多少天(我假设你只显示一年):
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// get the calendar
NSCalendar *calendar = [NSCalendar currentCalendar];
// get the date that points to the first day of the first month of the year containing the start date
// note that we include the era, because some calendars have more than an "AD"/"BC" kind of era
NSDateComponents *yearStartComponents = [calendar components:(NSEraCalendarUnit | NSYearCalendarUnit) fromDate:_startDate];
[yearStartComponents setMonth:1];
[yearStartComponents setDay:1];
NSDate *startOfYear = [calendar dateFromComponents:yearStartComponents];
NSDateComponents *yearDiff = [[NSDateComponents alloc] init];
[yearDiff setYear:1];
// add one year to that date
NSDate *startOfNextYear = [calendar dateByAddingComponents:yearDiff toDate:startOfYear options:0];
// figure out how many days were between the two dates
NSDateComponents *dayDiff = [calendar components:NSDayCalendarUnit fromDate:startOfYear toDate:startOfNextYear options:0];
return [dayDiff day];
}
然后当您需要单元格时,您可以使用indexPath的“行”作为从开始日期开始的天数:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = ...;
NSCalendar *calendar = [NSCalendar currentCalendar];
NSDateComponents *diff = [[NSDateComponents alloc] init];
[diff setDay:[indexPath row]];
NSDate *date = [calendar dateByAddingComponents:diff toDate:_startDate options:0];
NSLocale *locale = [NSLocale currentLocale];
NSString *formatted = [NSDateFormatter localizedStringFromDate:date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterNoStyle locale:locale];
[[cell textLabel] setText:formatted];
return cell;
}
这种方法有几个好处:
NSCalendar
支持格里高利,佛教,中国,希伯来,伊斯兰,伊斯兰民间,日本,中华民国,波斯,印度和ISO8601日历。NSDateFormatterStyles
,您将使用适当的简写符号表示日期,无论是“5/12/12”还是“12月12日”或完全不同的东西。