我希望能够在我的表格的标题中干净地设置我的日期。以下是我的代码:如何从NSFetchedResultsSectionInfo格式化日期?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
id <NSFetchedResultsSectionInfo> sectionInfo = [[self.fetchedResultsController sections] objectAtIndex:section];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
NSDate *date = [dateFormatter dateFromString:sectionInfo.name];
NSString *formattedDate = [dateFormatter stringFromDate:date];
NSLog(@"%@", formattedDate);
return formattedDate;
}
使用此代码,由于日期为空,因此没有节标题出现。出于某种原因,dateFromString无法将字符串sectionInfo.name转换为NSDate。有什么建议么?
你的字符串'date'格式化到什么程度? – 2012-03-12 22:21:42
+1塞巴斯蒂安的问题。只是我猜他是指section的标题字符串 - sectionInfo.name的格式。如果coredata正确读取所有内容,那么唯一可能导致错误的是格式不匹配。 – makaron 2012-03-12 22:37:47
嘿,我的部分标题上显示的字符串是在窗体中:2012-03-12 07:00:00 +0000我试图将其转换为2012年3月3日 – djblue2009 2012-03-12 22:56:45