我的核心数据堆栈中存储了两种类型的事件,每一种都有一个时间戳。我很感兴趣是否有一种在UITableView
with 部分中显示这些记录的好方法,其中每个部分都是任意长的(一天、一周等)。
有没有办法将核心数据对象的时间戳转换为部分标题,将一天中的小时数四舍五入?
所以我们会得到:
October 5 < section title
Record 1 < records displayed in the section
Record 2
Record 3
October 6
Record 4
October 7
Record 5
...
-OR-
Week 1
Record 1
Record 2
Week 2
Record 3
...
这是我目前用来实现这一目标的方法,但仅限于每天的每个部分。
但是可以说我没有考虑过这个要求,并且有一个只有时间戳的事件列表。我怎样才能把它们分成几部分?
//the method used to convert a date into a number to store with the event
-(int)getDateIDFromDate:(NSDate*)date
{
int gmtOffset = [[NSTimeZone localTimeZone] secondsFromGMT];
int dateID =([date timeIntervalSinceReferenceDate]+gmtOffset)/86400;
return dateID;
}
//when inserting a record, the number is saved
newManagedObject.dayID = [NSNumber numberWithInt:[self getDateIDFromDate:date]];
//when retrieving, the number is used as a section key path
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"dayID" cacheName:@"Day"];
//the number gets converted back into the date.
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
// Display the authors' names as section headings.
// return [[[dataManager.dreamEventsController sections] objectAtIndex:section] name];
NSString* dayIndex = [[[dataManager.fetchedResultsController sections] objectAtIndex:section] name];
int dayFromReferenceDate = dayIndex.intValue;
return [dataManager.sectionDateFormatter stringFromDate:[NSDate dateWithTimeIntervalSinceReferenceDate:(dayFromReferenceDate+1)*86400]];
}