如何更改从NSFetchedResultsControler填充的UITableview中各部分的显示顺序?

问题描述:

我想知道是否有人知道最好的解决方案来定义UITableView中的部分自定义显示顺序 - 我使用核心数据和NSFetchedResultsControler加载数据,我成功地使用NSSortDescriptors,并成功地将返回的项使用sectionNameKeyPath的部分:如何更改从NSFetchedResultsControler填充的UITableview中各部分的显示顺序?

到目前为止很好但是 - 这些部分按字母顺序列出,我想重写此部分并按特定的自定义排序顺序对部分进行排序。

例如,如果我有一个属性,叫做颜色的实体,称为项目,我用这个颜色属性作为sectionNameKeyPath:我怎么会再下订单的部分以特殊的方式 - 目前部分显示为

蓝, Green, Indigo, Orange, Red, Violet, Yellow。

  • 因为他们有NSortDescriptor应用于NSFetchedResultsController(我已经定义)。但我怎么能在显示部分,像这样一种特定的方式重新排列这些部分 -

红,橙 , 黄,绿 , 蓝,靛蓝 , 紫。

我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创建数组:上 所有已知的部分。

这是一种隐藏的方式,所以你错过了你第一次读到关于这堂课的几次。

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

+0

非常感谢,谢谢,我会潜入并给出一个去。 – user432841 2010-08-27 17:43:38

而不是存储字符串“红色”,“橙色”,“黄色”等,存储代表颜色的枚举值。因此,将0存储为红色,将1存储为橙色等,然后​​将其正确排序。当你想显示该部分的头部时,使用枚举找出它是哪种颜色并找到正确的字符串。