uitableview 中的部分和行不匹配

发布于 2024-11-26 00:07:57 字数 1300 浏览 1 评论 0原文

我使用 coredata 与此布局

在此处输入图像描述

现在我想显示一个表格,其中 Cave.title 按行排序在 Region.region as 部分中。使用以下代码,它可以正确显示部分和行。唯一的问题是顺序不正确。这些部分下面的行不属于该部分。

NSManagedObjectContext *context = country.managedObjectContext;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Cave" inManagedObjectContext:context];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"country = %@", country];
    request.fetchBatchSize = 20;

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                       initWithFetchRequest:request
                                       managedObjectContext:context
                                       sectionNameKeyPath:@"region.region"
                                       cacheName:nil];

    [request release];

    self.fetchedResultsController = frc;

我不确定应该向您展示 tableviewcontroller 的哪一部分?

在此处输入图像描述

“Bätterich”应在“其他”中,“Pertusio Acquacalda”应在“提契诺”中 我已经检查了 Core Data 的 sql 文件,引用就在那里。

I use coredata with this layout

enter image description here

Now I would like to show a table with the Cave.title as rows ordered in the Region.region as section. With the following code it shows the sections and the rows correctly. Only problem is that the order is not right. The rows below the sections don't belong to that section.

NSManagedObjectContext *context = country.managedObjectContext;

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    request.entity = [NSEntityDescription entityForName:@"Cave" inManagedObjectContext:context];
    request.sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"title" ascending:YES]];
    request.predicate = [NSPredicate predicateWithFormat:@"country = %@", country];
    request.fetchBatchSize = 20;

    NSFetchedResultsController *frc = [[NSFetchedResultsController alloc]
                                       initWithFetchRequest:request
                                       managedObjectContext:context
                                       sectionNameKeyPath:@"region.region"
                                       cacheName:nil];

    [request release];

    self.fetchedResultsController = frc;

I am not sure which part of the tableviewcontroller I should show to you?

enter image description here

"Bätterich" should be in "Others" and "Pertusio Acquacalda" should be in "Ticino"
I have checked Core Data's sql file and the references are right in there.

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

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

发布评论

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

评论(1

痴梦一场 2024-12-03 00:07:57

通过使用以下排序描述符解决了该问题

NSSortDescriptor *sortDescriptorRegion = [[NSSortDescriptor alloc] initWithKey:@"region.region" ascending:YES];
NSSortDescriptor *sortDescriptorCave = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorRegion, sortDescriptorCave, nil];

Solved the problem by using the following sortdescriptors

NSSortDescriptor *sortDescriptorRegion = [[NSSortDescriptor alloc] initWithKey:@"region.region" ascending:YES];
NSSortDescriptor *sortDescriptorCave = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptorRegion, sortDescriptorCave, nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文