之间我必须使用UICollectionView自定义日历设置,我有通过持有30天日期的数组。我试图做的是找出一个值是否在当前日期和它之前的7天之间。如果是我需要它做某件事。我无法弄清楚我做错了什么。以下是我迄今为止:检查,如果日期范围
NSDateFormatter *df = [NSDateFormatter new];
[df setDateFormat:@"MM-dd-yy"];
NSdate *date = [NSDate date];
NSDate *newCurrentDate = [date dateByAddingTimeInterval:+14*24*60*60];
NSDate *sevenDaysBeforeCurrent = [date dateByAddingTimeInterval:-7*24*60*60];
if([[df stringFromDate:[days objectAtIndex:indexPath.item]] compare:[df stringFromDate:newCurrentDate]] == NSOrderedAscending && [[df stringFromDate:[days objectAtIndex:indexPath.item]] compare:[df stringFromDate:sevenDaysBeforeCurrent]] == NSOrderedDescending)
{
UIImage *buttonImage = [UIImage imageNamed:@"dates.png"];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
}
它是什么改变每日期的形象newCurrentDate之前,而不是仅仅先前7.有人能看到我在做什么错?
继承人的数组的初始化:
myDays = [[NSMutableArray alloc] init];
NSCalendar *cal = [NSCalendar autoupdatingCurrentCalendar];
NSDate *today = [NSDate date];
date = [NSDate date];
for (int i = 0; i < 31; i++)
{
NSDateComponents *comps = [[NSDateComponents alloc]init];
[comps setDay:i];
[myDays addObject:[cal dateByAddingComponents:comps toDate:today options:0]];
}
为什么你要创建的字符串?你为什么不直接比较日期呢? – 2013-02-22 21:54:02
我有它最初[天objectAtindex:indexPath.item]比较:日期],但它会给予警告,当我将运行该应用程序会崩溃。 – bardockyo 2013-02-22 21:59:44
那么你在数组中没有'NSDate's。 – 2013-02-22 22:01:44