NSFetchedController:无序显示节

时间:2014-04-15 01:49:27

标签: ios core-data nssortdescriptor nsfetchedresultscontroller

我有一个Events实体的三个部分:

Upcoming  
Today    
Past     

这是FRC设置:

- (NSFetchedResultsController *)fetchedResultsController
{
    if(_fetchedResultsController!=nil)
    {
        return  _fetchedResultsController;
    }

    NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"Event"
                                              inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

     NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"modified"
                                                               ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc]initWithObjects:firstSort, nil];
     [fetchRequest setSortDescriptors:sortDescriptors];

    self.fetchedResultsController = [[NSFetchedResultsController alloc]initWithFetchRequest:fetchRequest
                                                                       managedObjectContext:self.managedObjectContext
                                                                         sectionNameKeyPath:@"sectionIdentifier"
                                                                                  cacheName:nil];
    self.fetchedResultsController.delegate = self;
    return self.fetchedResultsController;
}

对实体使用Transient属性,我将这些部分设置为:

- (NSString *)sectionIdentifier
{    
    [self willAccessValueForKey:@"sectionIdentifier"];
    NSString *tmp = [self primitiveSectionIdentifier];
    [self didAccessValueForKey:@"sectionIdentifier"];

    if (!tmp)
    {
        NSDate *dateToCompare = [self getUTCFormateDate:[self modified]];
        NSCalendar* calendar = [NSCalendar currentCalendar];
        NSDate* now = [NSDate date];
        NSDateFormatter *format = [[NSDateFormatter alloc] init];
        format.dateFormat = @"dd-MM-yyyy";
        NSString *stringDate = [format stringFromDate:now];
        NSDate *todaysDate = [format dateFromString:stringDate];

        NSUInteger differenceInDays =
        [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:dateToCompare] -
        [calendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSEraCalendarUnit forDate:todaysDate];


        NSString *sectionString;

        switch (differenceInDays) {
            case -1:
                sectionString = @"Past";
                break;
            case 0:
                sectionString = @"Today";
                break;
            case 1:
                sectionString = @"Upcoming";
                break;
            }

        tmp = sectionString;
        [self setPrimitiveSectionIdentifier:tmp];
    }

    return tmp;
}

目前sections显示在descending订单中,其中Upcoming是第一个,然后是Today,然后是Past。我想首先展示Today部分。

如何按以下顺序显示部分?

第0节:Today

第1节:Upcoming

第2节:Past

1 个答案:

答案 0 :(得分:1)

控制订单的唯一方法是你拥有的排序描述符:

 NSSortDescriptor *firstSort = [[NSSortDescriptor alloc] initWithKey:@"modified"
                                                           ascending:NO];

所以你需要改变它。您可以将其升序,但是您描述的排序顺序并不直接与日期相关联。

由于您所描述的内容取决于当前日期,因此您需要进行一些修改,以便将订单信息持久存储到模型中并且每天更新。然后,您可以将其添加为您的第一个排序描述符以使用它,并保留现有的排序描述符以便在每个部分内进行排序。

您需要存储的信息是根据@Volker的评论,每个部分都有一个简单的索引号。