【发布时间】:2015-03-08 19:12:39
【问题描述】:
我正在通过 MagicalRecord 使用 Core Data 中的数据填充表格视图。我希望表格视图按时间顺序显示,按日期划分,也按时间顺序显示。
首先,我从 transDate 中获取 sortDate,来自我在网上找到的修改代码:
// Gives beginning of this day
- (NSDate *)sortDateForDate:(NSDate *)inputDate
{
// Use the current calendar and time zone
NSCalendar *calendar = [NSCalendar currentCalendar];
NSTimeZone *timeZone = [NSTimeZone systemTimeZone];
[calendar setTimeZone:timeZone];
// Selectively convert the date components (year, month, day) of the input date
NSDateComponents *dateComps = [calendar components:NSCalendarUnitYear | NSCalendarUnitMonth | NSCalendarUnitDay fromDate:inputDate];
// Set the time components manually
[dateComps setHour:0];
[dateComps setMinute:0];
[dateComps setSecond:0];
// Convert back
NSDate *beginningOfDay = [calendar dateFromComponents:dateComps];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *beginningOfDayText = [formatter stringFromDate:beginningOfDay];
NSLog(@"The sortDate should be %@",beginningOfDayText);
return beginningOfDay;
}
然后我组装并保存新交易:
-(void) assembleAndSaveTransaction
{
NSManagedObjectContext *localContext = [NSManagedObjectContext MR_contextForCurrentThread];
self.thisTransaction.transDate = [NSDate date];
self.thisTransaction.paidTo = self.noteField.text;
self.thisTransaction.forWhat = self.forWhatField.text;
// Call to obtain sortDate, method above
self.thisTransaction.sortDate = [self sortDateForDate:self.thisTransaction.transDate];
[localContext MR_saveToPersistentStoreAndWait];
}
接下来,我获取所有交易,按属性“sortDate”分组并按属性“transDate”排序。
- (void)viewDidLoad
{
[super viewDidLoad];
transactionFRC = [WMMGTransaction MR_fetchAllGroupedBy:@"sortDate" withPredicate:nil sortedBy:@"transDate" ascending:NO];
}
然后,我将 sortDate 转换为字符串并将其分配给标题标题:
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSString *sectionLabel = [formatter stringFromDate:[transactionFRC.sections objectAtIndex:section]];
NSLog(@"The sortDate is %@",[formatter stringFromDate:[transactionFRC.sections objectAtIndex:section]]);
NSLog(@"The sectionLabel should be %@",sectionLabel);
return sectionLabel;
}
表格视图的显示方式如下:
数据似乎正确分组和排序。节标题出现在按时间顺序排列的适当位置。但是标题标题没有出现。
当我将这段代码放在带有相关TableView的VC的viewDidLoad方法中时:
NSLog(@"The header dates are as follows:\n");
for (NSDate *headerDate in [transactionFRC sections])
{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterFullStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
NSLog(@"The sortDate is %@",[formatter stringFromDate:headerDate]);
}
我看到了这个读数:
2015-03-08 12:06:00.502 WheredMyMoneyGo[5374:7322757] The header dates are as follows:
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
2015-03-08 12:06:00.503 WheredMyMoneyGo[5374:7322757] The sortDate is (null)
众所周知,我会做一些愚蠢的事情,但我一直在寻找这个 2 天没有成功。
有什么想法吗?
【问题讨论】:
标签: ios uitableview tableview nsfetchedresultscontroller