2012-06-19 39 views
1

我知道如何通過NsDate對tableview中的核心數據對象進行排序,但默認情況下這似乎爲每個對象都創建一個新節。我想用NSDateFormatter按照中間格式排列日期。我將如何做到這一點?核心數據按格式化日期排序tableview

例如,如果我在同一天創建了3個對象,我希望它們位於同一節中,節標題是那一天,不需要時間。

每個對象都有一個NSDate屬性。謝謝你的幫助。

這是我在fetchedResultsController中使用rgeorge的建議的代碼。我在這裏錯過了什麼?

- (NSFetchedResultsController *)fetchedResultsController { 

if (fetchedResultsController != nil) { 
    NSLog(@"get old fetched controller"); 

    return fetchedResultsController; 
} 

else{ 
    NSLog(@"get new fetched controller"); 
} 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"InTextEntity" inManagedObjectContext:managedObjectContext]; 
[fetchRequest setEntity:entity]; 
[fetchRequest setFetchBatchSize:20]; 

NSSortDescriptor *dateDescriptor = [[NSSortDescriptor alloc] initWithKey:@"dateModified" ascending:NO]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:dateDescriptor, nil]; 
[fetchRequest setSortDescriptors:sortDescriptors]; 


NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"mediumFormattedDate" cacheName:@"Root"]; 
aFetchedResultsController.delegate = self; 
self.fetchedResultsController = aFetchedResultsController; 

NSError *error = nil; 
if (![fetchedResultsController performFetch:&error]) { 

    NSLog(@"Unresolved error %@, %@", error, [error userInfo]); 

} 

return fetchedResultsController; 

}

回答

2

(我會如果你沒有,我建議檢查出來寫這件事假設你使用的NSFetchedResultsController來驅動你的tableview。)

NSFetchedResultsController的切片能力的一個有趣的特點:雖然你的排序上的屬性必須是一個建模屬性(因爲sqlite確實進行了排序),所以組的屬性不需要是。唯一的要求是分組與訂購一致。 (即,由sort屬性排序將會把對象與彼此相鄰的匹配組屬性。)

所以只需添加這樣的事情您的建模對象類:

// in interface 
@property (nonatomic, readonly) NSString *mediumFormattedDate; 

// in impl 
-(NSString *)mediumFormattedDate 
{ 
    // this can be fancier if you need a custom format or particular timezone: 
    return [NSDateFormatter localizedStringFromDate:self.date 
             dateStyle:NSDateFormatterMediumStyle 
             timeStyle:NSDateFormatterNoStyle]; 
} 

(無需提中的所有.xcdatamodel中的mediumFormattedDate。)

然後繼續並按日期屬性對對象進行排序,但將它們按您的新屬性進行分組。當您創建NSFetchedResultsController,沿着這些線路這麼做:

NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"MyFancyEntity"]; 
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"date" 
                ascending:YES]; 
[fr setSortDescriptors:[NSArray arrayWithObject:sd]]; 
NSFetchedResultsController *frc = 
[[NSFetchedResultsController alloc] initWithFetchRequest:fr 
            managedObjectContext:myManagedObjectContext 
             sectionNameKeyPath:@"mediumFormattedDate" 
               cacheName:nil]; 
// then do stuff with frc 

這一切都需要!我已在幾個應用程序中完成此操作以獲取日期分組,並且運行良好。

+0

非常感謝!我會在早上嘗試這個,讓你知道它是怎麼回事! –

+0

有一個問題,在mediumFormattedDate中設置self.date的值是什麼? –

+0

這是我得到的錯誤:實體InTextEntity不是關鍵字值「mediumFormattedDate」的編碼兼容。 –

0

聽起來你設置讀取的結果控制器上的部分索引是你的日期屬性,這似乎是不可取的。

相反,你應該自己計算段索引,然後按日期排序。您可以在數據模型中完成此操作,也可以通過代碼手動計算部分。例如,您可以向託管對象模型添加一個名爲「Day」的屬性,並將其設置爲您想要使用的任何值(您不指定它是否像星期一或實際日期,如21)。

然後,您可以將該屬性傳遞給獲取的結果控制器。

或者你可以自己實現這些部分,日子很簡單,它的週一到週日。日期有點困難,1-28,30,31取決於月份。然後使用適當的NSPredicate/NSFetchRequest獲取每個部分中項目的數量。