How do I account for empty items when sorting my UITableView?
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 appe开发者_开发知识库ars 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?
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.
精彩评论