对 UITableView 进行排序时如何考虑空项?

发布于 2024-11-01 09:38:11 字数 1067 浏览 0 评论 0原文

我有一个 Checklists 表,每个表都包含一堆 ChecklistItems。每个 ChecklistsItem 有 2 个 Bool 值:CheckedUrgent。 Checklist 实体还具有用于跟踪事物的各种属性(例如 itemsUnchecked)。

我想对我的表格进行排序,将最不完整的清单(即未检查项目最多的清单)放在顶部。我将排序描述符设置如下:

NSMutableArray *items = [NSMutableArray arrayWithArray:fetchedResultsController.fetchedObjects];
NSSortDescriptor *itemsUncheckedDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itemsUnchecked" ascending:NO];
NSSortDescriptor *itemsUrgentDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itemsUrgent" ascending:NO];
NSSortDescriptor *itemsCountDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checklistItems.@count" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemsUncheckedDescriptor, itemsUrgentDescriptor, itemsCountDescriptor, nil];
[items sortUsingDescriptors:sortDescriptors];

除了一件事之外,这一切都工作正常。如果清单为空(即其中还没有清单项目),它会出现在完整的清单下方(即所有项目均已勾选)。这是因为空清单有零个项目,所以被我的 itemsCountDescriptor 放置在底部。

如何使我的空清单显示在已完成的清单之上,但仍将项目数量作为我的最终描述符?

I have a table of Checklists, each of which contains a bunch of ChecklistItems. Each ChecklistsItem has 2 Bool values: Checked and Urgent. The Checklist entity also has various attributes I use to keep track of things (such as itemsUnchecked).

I want to sort my table with the least-complete checklists (i.e. those with the most unchecked items) at the top. I am setting my sort descriptors like this:

NSMutableArray *items = [NSMutableArray arrayWithArray:fetchedResultsController.fetchedObjects];
NSSortDescriptor *itemsUncheckedDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itemsUnchecked" ascending:NO];
NSSortDescriptor *itemsUrgentDescriptor = [[NSSortDescriptor alloc] initWithKey:@"itemsUrgent" ascending:NO];
NSSortDescriptor *itemsCountDescriptor = [[NSSortDescriptor alloc] initWithKey:@"checklistItems.@count" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:itemsUncheckedDescriptor, itemsUrgentDescriptor, itemsCountDescriptor, nil];
[items sortUsingDescriptors:sortDescriptors];

This all works fine except for one thing. If a checklist is empty (i.e. it has no checklist items in it at all yet), it appears below checklists that are complete (i.e. all their items are ticked). This is because empty checklists have zero items, so get placed at the bottom by my itemsCountDescriptor.

How can I make my empty checklists appear above the completed checklists, but still have the number of items as my final descriptor?

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

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

发布评论

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

评论(1

宫墨修音 2024-11-08 09:38:11

我能想到的最干净的事情就是声明另一个属性 BOOL isEmpty 并按该属性以及其他属性进行排序。

The cleanest thing I can think of off the top of my head is to declare another attribute BOOL isEmpty and sort by that as well as your other attributes.

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