我使用@ BoltClock的独角兽和@ Rog的anwser时有同样的问题。 简单地增加了一个短暂的NSString * sectionTitle我的管理对象,使用@“sectionTitle”作为sectionNameKeyPath和创建自定义的getter像这样:
-(NSString *)sectionTitle
{
NSDate *_now = [NSDate date];
NSDate *_today = [_now dateByAddingTimeInterval: -86400.0];
NSDate *_yesterday = [_now dateByAddingTimeInterval: -172800.0];
NSDate *_thisWeek = [_now dateByAddingTimeInterval: -604800.0];
NSDate *_lastWeek = [_now dateByAddingTimeInterval: -1209600.0];
NSDate *_thisMonth = [_now dateByAddingTimeInterval: -2629743.0];
// if better precision required use something more sophisticated for month...
double today = [_today timeIntervalSince1970];
double yesterday = [_yesterday timeIntervalSince1970];
double thisWeek = [_thisWeek timeIntervalSince1970];
double lastWeek = [_lastWeek timeIntervalSince1970];
double thisMonth = [_thisMonth timeIntervalSince1970];
[self willAccessValueForKey:@"timestamp"];
double ts = [self.timestamp timeIntervalSince1970];
[self didAccessValueForKey:@"timestamp"];
NSString *title = @"";
if(ts >= today) title = NSLocalizedString(@"TODAY",nil);
else if (ts >= yesterday) title = NSLocalizedString(@"YESTERDAY",nil);
else if (ts >= thisWeek) title = NSLocalizedString(@"THIS WEEK",nil);
else if (ts >= lastWeek) title = NSLocalizedString(@"LAST WEEK",nil);
else if (ts >= thisMonth) title = NSLocalizedString(@"THIS MONTH",nil);
return title;
}
谢谢,但那是如何显示节的标题的方式。但它不会将同一日期的两个对象分组。每个对象还有一个部分。 – 2010-12-12 09:45:21
够公平的,我已经编辑了我的答案,提出了一种可以尝试的替代方法。 – Rog 2010-12-12 20:56:43
非常感谢你Rog!这工作。谢谢! ;) – 2010-12-13 06:38:39