动态地将 UITableView 分成任意部分?

发布于 2024-11-07 10:55:03 字数 1201 浏览 0 评论 0原文

我有一个核心数据应用程序,它显示自定义对象列表,每个对象代表戏剧作品。

每个演出对象都有一堆属性 - 名称、徽标、开幕日期、演出类型。

我可以很好地检索它们,并按类型排序 - 但我希望每个节目类型在表中都有自己的部分。

我事先不知道结果集中将表示多少个节目类型,因此我最初不知道 numberofsections 应该是多少。

所以我想问题是 - 我将如何将结果集划分为按类型分组的部分?

目前我正在这样做:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Show" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: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:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

I have a core data application which displays a list of custom objects, each representing theatrical productions.

Each show object has a bunch of properties - name, logo, opening date, show type.

I can retrieve them fine, and sort by type just fine - but I would like each show type to be its own section in the table.

I don't know ahead of time how many show types will be represented in the result set, so I don't know initially what the numberofsections should be.

So I guess the question is - how would I go about dividing the result set into sections grouped by type?

Currently I'm doing this:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Show" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:20];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"type" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects: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:nil cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

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

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

发布评论

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

评论(1

摇划花蜜的午后 2024-11-14 10:55:03

我会编写 numberOfSections 方法,以便它动态检查类型的数量。然后,每当有新类型出现时,只需执行 [tableView reloadData];

I would write numberOfSections method so that it dynamically checks for the number of types. Then, whenever a new type rolls in, just do a [tableView reloadData];

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