1

我正在制作一个类似于 iPhone 日历 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;
} 
4

1 回答 1

3

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

Timothy Armes 已经遇到并解决了这个问题,他创建了一个TAFetchedResultsController允许空部分的类。它是NSFetchedResultsController. 它还允许您在不是部分名称的字段上对部分进行排序(非常方便)

但是,您将需要对您的核心数据模型进行更改 - 这并不是完全替代。

https://github.com/timothyarmes/TAFetchedResultsController

但它确实工作得很好,如果你愿意重新做你的数据模型,它将解决你的问题。

希望这可以帮助 :)

于 2012-07-02T06:06:28.703 回答