核心数据表视图部分使用 NSSortDescriptor 按工作日排序 [英] Core Data Table View Section Sort by weekdays using NSSortDescriptor
问题描述
我目前正在尝试将我的对象数组按天顺序排序,以便它们可以按正确的顺序分组,即星期一、星期二、星期三,然后按开始时间分组.
I'm currently trying to sort my array of objects into day order so they can be grouped in the correct order i.e. Monday, Tuesday, Wednesday then by start time.
唯一的问题是我不知道如何做到这一点,我的代码目前看起来像这样:按字母顺序然后按时间排序:
Only problem is I can't figure out how to do this, my code currently looks like this: Which sorts alphabetically then by time:
NSString *sectionKey = nil;
switch (tab) {
case kByWeekA: {
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:NO];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"starttime" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];
sectionKey = @"day";
break;
}
case kByWeekB:{
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"starttime" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1, sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];
sectionKey = @"day";
break;
}
default:
break;
}
请帮忙!
推荐答案
我看到两个解决方案:
- 为您的排序描述符创建自定义比较,查看day"键并根据需要对它们进行排序.请参阅指定自定义比较 此处.
- 将day"键设为整数,并按所需顺序对日期值使用枚举.
更新 2 选项 2 的示例代码:
UPDATE 2 Example code for option 2:
-将day"属性更改为数字(Int16)并添加瞬态(非持久性)dayName"字符串属性.
-Change the "day" attribute to a number (Int16) and add a transient (non-persistant) "dayName" string attribute.
-添加工作日的枚举:
typdef enum {
kMonday,
kTuesday,
kWednesday,
kThursday,
kFriday,
kSaturday,
kSunday
} WeekDay;
-将托管对象的day"属性设置为 NSNumber 对象.例如:
-Set the "day" property of your managed objects to be NSNumber objects. For example:
object.day = [NSNumber numberWithInteger:kMonday];
-为瞬态dayName"属性实现 getter 函数:
-Implement the getter function for the transient "dayName" property:
- (NSString*)dayName {
switch ((WeekDay)self.day.integerValue) {
case kMonday:
return @"Monday";
break;
case kTuesday:
return @"Tuesday";
break;
case kWednesday:
return @"Wednesday";
break;
case kMonday:
return @"Monday";
break;
case kThursday:
return @"Thursday";
break;
case kFriday:
return @"Friday";
break;
case kSaturday:
return @"Saturday";
break;
case kSunday:
return @"Sunday";
break;
}
}
-在fetch请求中,按day"排序,使用dayName"作为section名key路径.
-In the fetch request, sort by "day" and use "dayName" as the section name key path.
更新
注意:选项 1 适用于对数组进行排序,但 Core Data 为不受支持的 NSSortDescriptor 选择器:weekdayCompare:"抛出NSInvalidArgumentException"异常:
选项 1 的示例代码:
Example code for option 1:
// category on NSString for custom comparison
@interface NSString (WeekdayComparison)
- (NSComparisonResult)weekdayCompare:(NSString*)otherDay;
@end
@implementation NSString (WeekdayComparison)
- (NSComparisonResult)weekdayCompare:(NSString*)otherDay {
NSArray *weekDays = [NSArray arrayWithObjects:@"Sunday", @"Monday", @"Tuesday", @"Wednesday", @"Thursday", @"Friday", @"Saturday", nil];
NSUInteger selfIndex = [weekDays indexOfObject:self];
NSUInteger otherDayIndex = [weekDays indexOfObject:otherDay];
if (selfIndex < otherDayIndex) {
return NSOrderedAscending;
}
else if (selfIndex > otherDayIndex) {
return NSOrderedDescending;
} else {
return NSOrderedSame;
}
}
@end
要使用新的比较方法:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"day" ascending:YES selector:@selector(weekdayCompare:)];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[sortDescriptor release];
这篇关于核心数据表视图部分使用 NSSortDescriptor 按工作日排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!