Iphone 将所有日历日期插入uitableview。
我正在创建一个应用程序,需要将一年中的所有日期放入uitableview(每个日期都有自己的单元格)。是否有任何方法可以使用nsdate用每个日期填充每个单元格 如果需要显示从1月1日到12月31日的所有日期,则需要手动创建Iphone 将所有日历日期插入uitableview。,iphone,objective-c,ios4,Iphone,Objective C,Ios4,我正在创建一个应用程序,需要将一年中的所有日期放入uitableview(每个日期都有自己的单元格)。是否有任何方法可以使用nsdate用每个日期填充每个单元格 如果需要显示从1月1日到12月31日的所有日期,则需要手动创建NSDate实例 以下方法可能有助于处理UITableView单元格所需的NSDate实例: - (NSInteger)daysInYear:(NSInteger)year { if (year % 400 == 0) return 366; e
NSDate
实例
以下方法可能有助于处理UITableView
单元格所需的NSDate
实例:
- (NSInteger)daysInYear:(NSInteger)year {
if (year % 400 == 0) return 366;
else if (year % 100 == 0) return 365;
else if (year % 4 == 0) return 366;
else return 365;
}
- (NSDate *)firstDayInYear:(NSInteger)year {
NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
[fmt setDateFormat:@"MM/dd/yyyy"];
NSDate *firstDayInYear = [fmt dateFromString:[NSString stringWithFormat:@"01/01/%d", year]];
[fmt release];
return firstDayInYear;
}
- (NSDate *)dateWithDayInterval:(NSInteger)dayInterval sinceDate:(NSDate *)referenceDate {
static NSInteger SECONDS_PER_DAY = 60 * 60 * 24;
return [NSDate dateWithTimeInterval:dayInterval * SECONDS_PER_DAY sinceDate:referenceDate];
}
在tableView:cellforrowatinexpath:method中,您可以实例化相应的NSDate
实例:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSDate *dateForCell = [self dateWithDayInterval:indexPath.row sinceDate:self.firstDayInYear];
// ... rest of your method ...
}
这个问题的答案看起来很有用:非常感谢。这可能需要我再花一个星期的时间来弄清楚。一旦我最终停止使用kal Calendar,我注意到当我将其放入分组样式表视图时,它会在第七个位置停止(每个部分中的单元格数量),并在每个部分中重复。在分组样式uitableview中添加日期的最佳方式是什么?再次感谢你,我感谢你的帮助help@pythonquick这在uitableview所有行的同一天重复。