将空部分添加到NSFetchedResultsController

时间:2012-06-08 20:35:42

标签: objective-c nsfetchedresultscontroller

我正在制作一个像iPhone Calendar ListView一样的视图。我正在使用核心数据并获得约会并按日期对它们进行分组。

然而就像在iPhone列表视图中一样,我需要为今天添加一个空白部分,即使没有约会。我无法弄清楚如何为没有约会的部分执行此操作,因为我在创建分组之前进行了排序。

我如何向NSFetchedResultsController添加一个空白部分并使其使用,以便今天的日期位于正确的位置,而不是在列表的末尾?

- (NSFetchedResultsController *)fetchedResultsController {

    /*
     Set up the fetched results controller.
     */
    // Create the fetch request for the entity.
    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    // Edit the entity name as appropriate.
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Appointments" inManagedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext]];
    [fetchRequest setEntity:entity];
    //[fetchRequest setIncludesPendingChanges:YES];

    // Set the batch size to a suitable number.
    //[fetchRequest setFetchBatchSize:20];

    // Sort using the date / then time property.
    NSSortDescriptor *sortDescriptorDate = [[NSSortDescriptor alloc] initWithKey:@"date" ascending:YES];
    NSSortDescriptor *sortDescriptorTime = [[NSSortDescriptor alloc] initWithKey:@"start_time" ascending:YES selector:@selector(localizedStandardCompare:)];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorDate, sortDescriptorTime, nil];


    [fetchRequest setSortDescriptors:sortDescriptors];

    // Use the sectionIdentifier property to group into sections.
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[[CoreDataHelper sharedInstance] managedObjectContext] sectionNameKeyPath:@"date" cacheName:nil];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;
    return fetchedResultsController;
} 

1 个答案:

答案 0 :(得分:3)

你不能拥有NSFetchedResultsController的空白部分 - 这就是它目前的设计方式,我称之为限制:)

Timothy Armes遇到并解决了这个问题,他创建了一个类TAFetchedResultsController,允许空白部分。它是NSFetchedResultsController的替代品。它还允许您对不是部分名称的字段(非常方便)

对您的部分进行排序

但是,您需要对Core Data模型进行更改 - 它不是替代品。

https://github.com/timothyarmes/TAFetchedResultsController

但它确实运行良好,并且如果您愿意重新制作数据模型,它将解决您的问题。

希望这会有所帮助:)

相关问题