您好,我对 iOS 开发比较陌生(如果这个问题已经得到解决,请提前抱歉),我一直是这个应用程序中的这个子模块,用户必须输入每日数值数据,然后列表显示在表格视图中。问题是我正在尝试按 MMMM yyyy 格式的 NSdate 变量“inputDate”进行排序(例如:名为:2013 年 2 月的部分)。但是,即使在同一个月输入了一个值,它也不会将它们分组在相同的部分中(请参阅末尾的链接以获取说明)
以下是似乎与问题相关的代码部分:
//The Fetch result controller
- (NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
// Create and configure a fetch request with the PVMessWert entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"PVMessWert" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
// Create the sort descriptors array.
NSSortDescriptor *datumsort = [[NSSortDescriptor alloc] initWithKey:@"inputDate" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:datumsort, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Create and initialize the fetch results controller.
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"inputDate" cacheName:@"Root"];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
然后:titleForHeaderInSection:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSArray *objects = [sectionInfo objects];
NSManagedObject *managedObject = [objects objectAtIndex:0];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setDateFormat:@"MMMM yyyy"];
NSDate *headerDate = (NSDate *)[managedObject valueForKey:@"inputDate"];
NSString *headerTitle = [formatter stringFromDate:headerDate];
return headerTitle;
}
然后,Dateformatter 函数:
- (NSDateFormatter *)dateFormatter
{
static NSDateFormatter *dateFormatter = nil;
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}
为了说明问题,一个 detailledListView 的小屏幕截图
网址:http: //i.stack.imgur.com/j8Kvz.png
预先感谢您的帮助。