我希望能夠在我的表格的標題中乾淨地設置我的日期。以下是我的代碼:如何從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