1

我想模仿iPhone日曆列表視圖並在列表中顯示我的約會。無論我是否有「今天」的約會,我都希望在今天的日期有一個部分。與我下面的fetchController如何檢查,然後如果今天沒有約會存在,添加一個空的部分,而不是搞亂排序?從核心數據中顯示空的UITableView行

- (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; 
} 

回答

0

你可以試試[context countForFetchRequest:request error:&error];如果沒有結果,這將返回0。由於您使用的是NSFetchedResultController,因此您必須在tableView方法中執行一些操作,以顯示不在結果控制器中的內容。因此,您可以先執行countForFetchRequest,然後如果它是0,請繼續並在表中顯示您需要的內容。

+0

那麼我需要的是顯示不帶行頭爲當前日期,如果他們是當前的日期沒有行。 – Bot

相關問題