1

我正在尝试模仿 iPhone 日历列表视图并在列表中显示我的约会。不管我是否有“今天”的约会,我都希望有一个包含今天日期的部分。使用下面的 fetchController 我将如何检查,然后如果今天没有约会,添加一个空白部分而不是弄乱排序?

- (NSFetchedResultsController *)fetchedResultsController {

//    if (fetchedResultsController != nil) {
//        return 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

2 回答 2

1

似乎这不能用 来完成NSFetchedResultsController,但是有一个 3rd 方类可以操作这些数据,因此您可以添加空行。

https://github.com/timothyarmes/TAFetchedResultsController

于 2012-08-16T20:55:12.087 回答
0

[context countForFetchRequest:request error:&error];如果没有结果,您可以尝试这将返回 0。由于您使用的是 NSFetchedResultController,因此您必须在 tableView 方法中进行一些操作,以便显示结果控制器中没有的内容。所以你可以先做 e countForFetchRequest ,然后如果它是 0 继续在表格中显示你需要的东西。

于 2012-05-23T01:24:46.987 回答