对 UITableView 进行排序时如何考虑空项?
我有一个 Checklists
表,每个表都包含一堆 ChecklistItems
。每个 ChecklistsItem 有 2 个 Bool 值:Checked
和 Urgent
。 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能想到的最干净的事情就是声明另一个属性 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.