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

发布于 2024-09-15 22:50:28 字数 3069 浏览 4 评论 0原文

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

到目前为止一切顺利,但是 - 这些部分按字母顺序列出,我想覆盖它并以特定的自定义排序顺序对这些部分进行排序。

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

蓝色, 绿色的, 靛青, 橙子, 红色的, 紫色, 黄色的。

  • 因为它们有一个 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 的属性的示例:但这些示例仍然倾向于提供然后按字母顺序列出的节名称。

显然我对此很陌生,但任何建议/方向将不胜感激。

I was wondering if anyone knew the best solution to defining a custom display order for the sections in a UITableView - I am loading the data using Core Data and an NSFetchedResultsControler, and I am successfully using NSSortDescriptors and also successfully grouping the items returned into sections using sectionNameKeyPath:

So far so good BUT - the sections are listed in alphabetical order and I would like to override this and order the sections in a specific custom sort order.

For example if I had a Entity called Item with an attribute called colour and I used this colour attribute as the sectionNameKeyPath: how would I then order the sections in a specific way - currently the sections display as

Blue,
Green,
Indigo,
Orange,
Red,
Violet,
Yellow.

  • because they have an NSortDescriptor applied to the NSFetchedResultsController (which I have defined). BUT how could I reorder these sections in a specific way to display the sections like so -

Red,
Orange,
Yellow,
Green,
Blue,
Indigo,
Violet.

My NSFetchedResultsController code follows -

- (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_;
}

I understand the examples posted regarding using a custom subclass of NSManagedObject and a getter method to dynamically assign a property to be used as the sectionNameKeyPath: but the examples for these still tend to provide section names which are then listed alphabetically.

It's clearly me being new to this but any suggestions/direction would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

jJeQQOZ5 2024-09-22 22:50:28

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

来自 NSFetchedResultsController 类文档:

如果您想自定义章节和索引标题的创建,则可以创建此类的子类。你覆盖
sectionIndexTitleForSectionName:如果
您希望部分索引标题为
除大写字母外的其他内容
节名称的第一个字母。你
覆盖sectionIndexTitles如果你
希望索引标题是某种东西
除了创建的数组之外
呼叫
sectionIndexTitleForSectionName: 上
所有已知部分。

它有点隐藏,所以你会怀念最初几次阅读有关该课程的内容。

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

To provide a custom sort order to sections you need to subclass NSFetchedResultsController.

From the NSFetchedResultsController class docs:

You create a subclass of this class if you want to customize the creation of sections and index titles. You override
sectionIndexTitleForSectionName: if
you want the section index title to be
something other than the capitalized
first letter of the section name. You
override sectionIndexTitles if you
want the index titles to be something
other than the array created by
calling
sectionIndexTitleForSectionName: on
all the known sections.

It's kind of tucked away so you miss the first couple of times you read about the class.

To implement, get the fetched results controller's section attribute which is an array of section names sorted alphabetically, then return the right element for the index of the tableview section.

写给空气的情书 2024-09-22 22:50:28

不存储字符串“Red”、“Orange”、“Yellow”等,而是存储表示颜色的枚举值。所以红色存储0,橙色存储1,等等。然后它就会正确排序。当您想要显示该部分的标题时,请使用枚举来确定它的颜色并找到正确的字符串。

Instead of storing the string "Red", "Orange", "Yellow" etc, store enum values that represents the colors. So store 0 for red and 1 for orange, etc. Then it will be sorted correctly. When when you want to display the header for the section use the enum to figure out which color it is and find the correct string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文