我使用transient attribute
來分發core data objects
table view sections
。 transient attribute
被稱爲sectionIdentifier它是根據名爲todoDueDate的屬性定義的。 我需要的方式來篩選對象都必須包含在以下組的一個,只有在一個:NSDate篩選案例
1. sectionIdentier = 0, **OVERDUE** , todoDueDate < today
2. sectionIdentier = 1, **TODAY** , todoDueDate = today
3. sectionIdentier = 2, **TOMORROW** , todoDueDate >today AND todoDueDate= = tomorrow
4. sectionIdentier = 3, **UPCOMING** , todoDueDate > today AND todoDueDate!=tomorrow
隨着我當前的代碼,我能夠過濾對象爲案例1,2和4(我必須在這裏放棄明天的對象)。我已經嘗試了幾種方法來過濾案例3的對象,但沒有成功。 我懇請您幫我過濾的情況下,3 這裏是我當前的代碼:
-(NSString *)sectionIdentifier{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp){
NSDate *date = self.todoDueDate;
NSDate *todayDate = [NSDate date];
NSLog(@"date= %@",date);
NSLog(@"todayDate = %@",todayDate);
NSCalendar *calendar = [NSCalendar currentCalendar];
NSInteger comps = (NSDayCalendarUnit | NSMonthCalendarUnit | NSYearCalendarUnit);
NSDateComponents *date1Components = [calendar components:comps fromDate:date];
NSDateComponents *date2Components = [calendar components:comps fromDate:todayDate];
date = [calendar dateFromComponents:date1Components];
todayDate = [calendar dateFromComponents:date2Components];
if([date
compare:todayDate] == NSOrderedAscending) {
tmp = @"0";//OVERDUE
}
if([date
compare:todayDate] == NSOrderedDescending) {
tmp = @"3";//UPCOMING BUT NOT TOMORROW(PENDING THIS PART)
}
if ([date
compare:todayDate] == NSOrderedSame) {
tmp = @"1";//TODAY
}
//TOMORROW COMES HERE
NSLog(@"Tmp= %@",tmp);
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}
這是我更新的代碼:
-(NSString *)sectionIdentifier{
[self willAccessValueForKey:@"sectionIdentifier"];
NSString *tmp = [self primitiveValueForKey:@"sectionIdentifier"];
[self didAccessValueForKey:@"sectionIdentifier"];
if (!tmp){
NSDate *today = [NSDate date];
NSDate *date = self.todoDueDate;
NSCalendar *calendar;
NSInteger daysAfterToday = [calendar components:NSDayCalendarUnit
fromDate:today toDate:date options:0].day;
// NSString *section;
if (daysAfterToday < 0) {
tmp = @"0";
} else if (daysAfterToday == 0) {
tmp = @"1";
} else if (daysAfterToday == 1) {
tmp = @"2";
} else {
tmp = @"3";
}
NSLog(@"TODAY = %@", today);
NSLog(@"DATE = %@", date);
NSLog(@"DAYS AFTER TODAY = %ld",(long)daysAfterToday);
NSLog(@"Tmp= %@",tmp);
[self setPrimitiveValue:tmp forKey:@"sectionIdentifier"];
}
return tmp;
}
這裏記錄的結果:
2014-01-17 22:35:53.576 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.619 To-Do Pro Light[4124:a0b] DATE = 2014-01-13 04:08:32 +0000
2014-01-17 22:35:53.656 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.664 To-Do Pro Light[4124:a0b] Tmp= 1
2014-01-17 22:35:53.739 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.743 To-Do Pro Light[4124:a0b] DATE = 2014-01-18 04:13:39 +0000
2014-01-17 22:35:53.748 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.752 To-Do Pro Light[4124:a0b] Tmp= 1
2014-01-17 22:35:53.757 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.762 To-Do Pro Light[4124:a0b] DATE = 2014-01-19 04:16:14 +0000
2014-01-17 22:35:53.766 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.770 To-Do Pro Light[4124:a0b] Tmp= 1
2014-01-17 22:35:53.776 To-Do Pro Light[4124:a0b] TODAY = 2014-01-18 05:35:53 +0000
2014-01-17 22:35:53.779 To-Do Pro Light[4124:a0b] DATE = 2014-01-25 04:11:34 +0000
2014-01-17 22:35:53.825 To-Do Pro Light[4124:a0b] DAYS AFTER TODAY = 0
2014-01-17 22:35:53.831 To-Do Pro Light[4124:a0b] Tmp= 1
我也會嘗試你的建議,實現起來要容易得多,只需要12條代碼。非常聰明。 – mvasco
+1確實非常巧妙 –
我已經包含了應該在四個可能組中排序的對象,但啓動應用程序後,所有對象都包含在組1中...... – mvasco