2013-05-09 52 views
0

我正在使用NSFetchedResultsController,我想根據我的實體中的日期字段在我的表中創建兩個部分。我不想根據單個日期進行分組,但我希望有一個部分的日期字段爲零,其中一個部分的日期字段不爲零。NSFetchedResultsController創建部分的日期爲零

有沒有辦法使用sectionNameKeyPath來實現這個目標?如果不是,我應該如何根據零值和非零值來分割我提取的結果表?

回答

1

我以爲有以下應該工作:

  • 使用您date字段作爲第一個排序描述符。
  • 定義一個瞬態屬性sectionIdentifier並使用它作爲sectionNameKeyPath
  • 定義僅返回「0」或「1」的瞬態屬性的getter函數。在最簡單的情況下(不緩存),它是這樣的:

    - (NSString *)sectionIdentifier 
    { 
        if (self.date == nil) 
         return @"0"; 
        else 
         return @"1"; 
    } 
    
  • 實現自定義titleForHeaderInSection

    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { 
        id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section]; 
        if ([[sectionInfo name] isEqualToString:@"0"]) 
         return @"Date is nil"; 
        else 
         return @"Date is not nil"; 
    } 
    
+0

我還沒有得到片刻測試了這一點呢,但我沒有忘記你。我會盡快給你回覆。 – 2013-05-10 05:58:09