如何更改从NSFetchedResultsController填充的UITableview中节的显示顺序?

如何更改从NSFetchedResultsController填充的UITableview中节的显示顺序?,uitableview,core-data,nsfetchedresultscontroller,Uitableview,Core Data,Nsfetchedresultscontroller,我想知道是否有人知道为UITableView中的节定义自定义显示顺序的最佳解决方案-我正在使用Core data和NSFetchedResultsController加载数据,我正在成功地使用NSSortDescriptors,并且还成功地使用sectionNameKeyPath将返回的项分组到节中: 到目前为止还不错,但是-这些部分是按字母顺序列出的,我想覆盖它,并按照特定的自定义排序顺序对这些部分进行排序 例如,如果我有一个名为Item的实体,它有一个名为color的属性,并且我使用这个co

我想知道是否有人知道为UITableView中的节定义自定义显示顺序的最佳解决方案-我正在使用Core data和NSFetchedResultsController加载数据,我正在成功地使用NSSortDescriptors,并且还成功地使用sectionNameKeyPath将返回的项分组到节中:

到目前为止还不错,但是-这些部分是按字母顺序列出的,我想覆盖它,并按照特定的自定义排序顺序对这些部分进行排序

例如,如果我有一个名为Item的实体,它有一个名为color的属性,并且我使用这个color属性作为sectionNameKeyPath:那么我将如何以特定的方式对这些部分进行排序?当前,这些部分显示为

蓝色, 绿色 靛蓝, 橙色 红色 紫罗兰, 黄色

  • 因为他们有一个应用于NSFetchedResultsController(我已经定义了)的NSortDescriptor。但是我怎样才能以一种特定的方式对这些部分进行重新排序,以便像这样显示这些部分呢-
红色, 橙色 黄色的, 绿色 蓝色 靛蓝, 紫罗兰

我的NSFetchedResultsController代码如下-

- (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:@"Item" inManagedObjectContext:self.managedObjectContext];
    [fetchRequest setEntity:entity];

    // Set the batch size to a suitable number.
    [fetchRequest setFetchBatchSize:20];

    // Edit the sort key as appropriate.

    NSSortDescriptor *colourDescriptor = [[NSSortDescriptor alloc] initWithKey:@"colour" ascending:YES];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:colourDescriptor, sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    // Edit the section name key path and cache name if appropriate.
    // nil for section name key path means "no sections".
    NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"colour" cacheName:@"Root"];
    aFetchedResultsController.delegate = self;
    self.fetchedResultsController = aFetchedResultsController;

    [aFetchedResultsController release];
    [fetchRequest release];
    [sortDescriptor release];
    [colourDescriptor release];
    [sortDescriptors release];

    NSError *error = nil;
    if (![fetchedResultsController_ performFetch:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    return fetchedResultsController_;
}
我理解关于使用NSManagedObject的自定义子类和getter方法动态分配要用作sectionNameKeyPath的属性的示例:但是这些示例仍然倾向于提供按字母顺序列出的节名称


很明显,我是新来的,但如果有任何建议/指导,我将不胜感激

若要为节提供自定义排序顺序,您需要子类化
NSFetchedResultsController

NSFetchedResultsController
类文档:

如果要自定义节和索引标题的创建,可以创建此类的子类。你超越 sectionIndexTitleForSectionName:if 您希望节索引标题为 不是大写的东西 节名称的第一个字母。你 如果需要,请覆盖sectionIndexTitles 希望索引标题是什么 而不是由创建的数组 使命感 sectionIndexTitleForSectionName:打开 所有已知的部分

它有点藏起来了,所以你错过了最初几次关于这门课的阅读


要实现,请获取获取的结果控制器的
属性,该属性是按字母顺序排序的节名称数组,然后返回tableview节索引的正确元素

若要为节提供自定义排序顺序,您需要子类化
NSFetchedResultsController

NSFetchedResultsController
类文档:

如果要自定义节和索引标题的创建,可以创建此类的子类。你超越 sectionIndexTitleForSectionName:if 您希望节索引标题为 不是大写的东西 节名称的第一个字母。你 如果需要,请覆盖sectionIndexTitles 希望索引标题是什么 而不是由创建的数组 使命感 sectionIndexTitleForSectionName:打开 所有已知的部分

它有点藏起来了,所以你错过了最初几次关于这门课的阅读


要实现,请获取获取的结果控制器的
属性,该属性是按字母顺序排序的节名称数组,然后返回tableview节索引的正确元素

存储表示颜色的枚举值,而不是存储字符串“红色”、“橙色”、“黄色”等。因此,存储0表示红色,存储1表示橙色,等等,然后它将被正确排序。当您要显示节的标题时,请使用枚举确定其颜色并找到正确的字符串

存储表示颜色的枚举值,而不是存储字符串“红色”、“橙色”、“黄色”等。因此,存储0表示红色,存储1表示橙色,等等,然后它将被正确排序。当您要显示节的标题时,请使用枚举确定其颜色并找到正确的字符串