【问题标题】:NSFetchedResultsController create sections where date is nilNSFetchedResultsController 创建日期为零的部分
【发布时间】:2013-05-09 03:10:11
【问题描述】:

我正在使用 NSFetchedResultsController,我想根据我实体中的日期字段在我的表中创建两个部分。我不想根据单个日期进行分区,但我希望有一个日期字段为 nil 的部分和一个日期字段不为 nil 的部分。

有没有办法使用sectionNameKeyPath 来实现这一点?如果不是,我应该如何根据 nil 与非 nil 值对获取的结果表进行分区?

【问题讨论】:

    标签: ios ios6 nsfetchedresultscontroller key-value-coding


    【解决方案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";
      }
      

    【讨论】:

    • 我还没来得及测试一下,但我没有忘记你。我会试一试并尽快回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多