从核心数据中显示空的UITableView行

时间:2012-05-03 20:28:14

标签: objective-c uitableview core-data ios5 nsfetchedresultscontroller

我正在尝试模仿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;
} 

2 个答案:

答案 0 :(得分:1)

似乎不能使用NSFetchedResultsController完成此操作,但是有一个第三方类操作此数据,因此您可以添加空行。

https://github.com/timothyarmes/TAFetchedResultsController

答案 1 :(得分:0)

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