我對排序我的NSFetchedResultsController
有一個小問題。我的NSManagedObject
有兩個屬性。 date
和startTime
。CoreData NSFetchedResultsController排序
date
對我的所有物體都是時間00:00:00,這樣當使用date
作爲sectionNameKeyPath
時,它會將具有相同日期(按天)的所有對象抓取到一個部分中。如果日期的時間不同,它會將每個對象放入不同的部分。
這很好,但然後在每個組內,我想通過startTime
排序對象。因此,他們從date
到最新的每個部分的最早列表。
我的問題是當使用date
作爲sectionNameKeyPath
和startTime
作爲NSSortDescriptor`它不喜歡它並且奇怪地玩。比如只是有時以某種不規則的方式顯示某些數據。
我認爲這歸結爲必須使排序描述符和sectionNameKeyPath
相同。我在想這個嗎?如果不是,我應該如何設置我的NSFetchedResultsController
以提到的方式列出我的數據?
謝謝。
編輯:這裏是代碼...使用startTime
作爲我的第二個排序描述符時也值得注意,它會導致重複顯示在我的tableview與零對象。
NSFetchedResultsController
:
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"startTime" ascending:YES];
NSArray *sortDescriptors = @[sortDescriptor1, sortDescriptor2];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"date" cacheName:@"Master"];
cellForRowAtIndexPath
只是展示我如何指定各管理對象的一個片段:
id <NSFetchedResultsSectionInfo> sectionInfo = [self.flightFetchedResultsController.sections objectAtIndex:indexPath.section];
NSArray *sectionFlights = [sectionInfo objects];
Flight *flight = [sectionFlights objectAtIndex:indexPath.row];
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return self.flightFetchedResultsController.sections.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.flightFetchedResultsController sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
顯示一些代碼。 'startTime'是一個字符串嗎? (讓它成爲約會)。 – Wain
這是一個日期,代碼傳入... –
你正在得到錯誤的對象。我會更新我的答案。 – Fogmeister